title: Redirect not logged in users only on a specific page
body: <p>I want to redirect not logged in users in WordPress from page A to page B.</p>
  
  <p>I've tried every answer on StackOverflow, but they were all written years ago and none of them works.</p>
  
  <pre class="lang-php prettyprint-override"><code>add_action( 'template_redirect', 'redirect_to_specific_page' );
  
  function redirect_to_specific_page() {
  
      if ( is_page('slug') &amp;&amp; ! is_user_logged_in() ) {
  
          wp_redirect( 'http://www.example.com/sign-in/', 301 );
          exit;
      }
  }
  </code></pre>
  
  <p>When added to functions.php nothing happens.</p>