Hide BuddyPress pages and bbPress forums from not logged in users

Redirect not logged in users who click BuddyPress or bbPress links.
Redirect not logged in users who click BuddyPress or bbPress links.

The following code snippets will redirect a not logged in users when they click a BuddyPress or a bbPress link.
There is some variation between these depending on what you need. The following are code snippets need to be added to a child theme functions.php file or into a code snippet plugin. Such as Code Snippets or CodeKit – Custom Codes Editor.

When a user who is not logged in clicks a BuddyPress or bbPress link they will be redirected to an information page. Except for BuddyPress register and activation pages.

The below adds an if statement to check to see if it is a BuddyPress page or a BuddyPress user or if it is bbPress.
If so then move on to to the next if statement. Not logged in user. Not a BuddyPress register page. Not a BuddyPress activation page. Then redirect user to a specific page. (A logged in user, or clicking a BuddyPress register page or a BuddyPress activation page will not get redirected.)


Change the below redirect URL.

// Protect BuddyPress pages and bbPress forums: Not logged in users get redirected to an information page.
function ps_guest_redirect() {
     global $bp;
  if ( is_buddypress() or bp_is_user() or is_bbpress() ) {
      if(!is_user_logged_in() and !bp_is_register_page() and !bp_is_activation_page()) {
           wp_redirect('https://website.com/not-logged-in-users');
  exit;
      }
  }
}
add_filter('get_header','ps_guest_redirect',1);

When a user who is not logged in clicks a BuddyPress or bbPress link they will be redirected to two different information pages.

Change the below two redirect URL’s.

// Protect BuddyPress pages and bbPress forums: Not logged in users get redirected to two different information pages.
function ps_guest_redirect() {
   global $bp;
   if ( is_buddypress() or bp_is_user() ) {
     if(!is_user_logged_in() and !bp_is_register_page() and !bp_is_activation_page()) {
      wp_redirect('https://website.com/not-logged-in-buddypress-users/');
   exit;
      }
  }
  if(is_bbpress()){
     if(!is_user_logged_in()) {
     wp_redirect('https://website.com/not-logged-in-bbPress-users/');
  exit;
     }
  }
}
add_filter('get_header','ps_guest_redirect',1);

Redirect bbPress private forum link to an information page.

As you might have noticed if you use Private forums. The above code snippets will not redirect a Private forum to an information page. It will instead show a “Nothing here It looks like nothing was found at this location. Maybe try a search?” (a 404 error page.) To also redirect Private forums to an information page add the following code snippet.

You might need to change forums to the slug that you use.
Change the $location URL.

// Redirect private bbPress forums to an information page.
add_action('template_redirect', 'private_content_redirect_to_login', 9);
function private_content_redirect_to_login() {
  global $wp_query,$wpdb;
  if (is_404() and !is_user_logged_in()) {
    $private = $wpdb->get_row($wp_query->request);
    $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    $path = dirname($_SERVER['REQUEST_URI']);
    $forums = "forums";
    $location = ("https://website.com/not-logged-in-users");
    if( 'private' == $private->post_status  ) {
      wp_safe_redirect($location);
      exit;
    }
    }
    if(strpos( $path, $forums ) !== false){
      wp_safe_redirect($location);
      exit;
  }
}

Resources:
The BuddyPress Slack channel. A huge thank you to Prashant for helping me with the code!
wordpress.slack.com/archives/C02RQBYUG/p1538777140000100

stackoverflow.com/questions/47196124/bbpress-private-forum-redirect-to-login
seventhqueen.com/blog/code-snippets/restrict-guest-users-from-accessing-buddypress-or-bbpress-pages.html

This tutorial was originally written 21 October 2018.

Share the article:

Leave a Reply

Your email address will not be published.

10 + 13 =