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;
}
}