title: bbPress Private Forum Redirect to Login
body: <p><strong>Edit:</strong> Here is how to do it if you use a slug prefix for your forums (change "forums" if using a different slug).  This code also redirects the user to login and back to the page they were on for private pages / posts.</p>
  
  <pre><code>add_action('template_redirect', 'private_content_redirect_to_login', 9);
  function private_content_redirect_to_login() {
    global $wp_query,$wpdb;
    if (is_404()) {
      $private = $wpdb-&gt;get_row($wp_query-&gt;request);
      $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
      $path = dirname($_SERVER['REQUEST_URI']);
      $forums = "forums";
      $location = wp_login_url($_SERVER["REQUEST_URI"]);
      if( 'private' == $private-&gt;post_status  ) {
        wp_safe_redirect($location);
        exit;
      }
      }
      if(strpos( $path, $forums ) !== false){
        wp_safe_redirect($location);
        exit;
    }
  }
  </code></pre>
  
  <p>I am trying to get WordPress to redirect a user from the default 404 page that is shown when a user is not logged in but are trying to see a private topic. I'm very new to PHP and I cannot figure out why the below code is not working, it is also suppose to redirect them to the page they were trying to see after login. I've seen this question asked a few times but no answers.</p>
  
  <pre><code>add_action('template_redirect', 'private_content_redirect_to_login', 9);
  function private_content_redirect_to_login() {
    global $wp_query,$wpdb;
    if (is_404()) {
      $private = $wpdb-&gt;get_row($wp_query-&gt;request);
      $location = wp_login_url($_SERVER["REQUEST_URI"]);
      if( 'private' == $private-&gt;bbp_private_post_status  ) {
        wp_safe_redirect($location);
        exit;
      }
    }
  }
  </code></pre>
  
  <p>Thank you!</p>