I am helping to create a website for a human rights organization in Angola. They use BuddyPress for having members overview and bbPress for the forums. I added a private forum link in the menu so that when the user signs in they have easy access to it. That also meant that I had to redirect not logged in users.
To hide all BuddyPress pages so that they do not happen to become visible for non logged in users I used this code that I added to the child theme functions.php file.
// 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() || bp_is_user() || is_bbpress() ) { if(!is_user_logged_in()) { wp_redirect(' https://direitoshumanosangola.org/not-logged-in/'); exit; } } } add_filter('get_header','ps_guest_redirect',1);
The above code:
If it is buddypress or bp user or is bbpress and user is not logged inn then redirect to a not logged in page.
Code where BuddyPress redirects to one page and bbPress redirects to another page.
function ps_guest_redirect() { global $bp; if ( is_buddypress() || bp_is_user() ) { if(!is_user_logged_in()) { wp_redirect('https://direitoshumanosangola.org/not-logged-in/'); exit; } } if(is_bbpress()){ if(!is_user_logged_in()) { wp_redirect('https://direitoshumanosangola.org/'); exit; } } } add_filter('get_header','ps_guest_redirect',1);
The problem with the above code is that bbPress private forums will give a 404 error code and not redirect. To also redirect a private bbPress forum use the following code:
// Redirect private bbPress forum to specific 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()) { $host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $path = dirname($_SERVER['REQUEST_URI']); $forums = "forum"; $location = "https://direitoshumanosangola.org/not-logged-in/"; } if(strpos( $path, $forums ) !== false){ wp_safe_redirect($location); exit; } }
The only thing you need to adjust is the $location url. It should then work nicely.
NB! Remember to clear your cache to see the changes.
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
Redirect private bbPress forum to specific page works perfectly nice!
Thank you very much for this!
Thank you for your support of this extremely complex code problem, the information is extremely helpful!
Thank you very much for your help here. I have been struggling with the problem of the index page showing up when you type in the
(www.mywebsite/forums) . As our forum is just for members only, it is a problem with data protection if topics/names can be found that easily! I don’t really understand why the option isn’t included in the BBPress plugin. I couldn’t find anything in the BBPress forums. In any case, I really appreciate that you took the time to share this! Have a good day.
Thanks a lot! This code blocks access to the registration form as well, do you know how to keep the form public?
Hello
Thank you for your site
I had a problem with the page builder wpbakery of eternity
Thank you for helping me
thank you so much. hope you help people like me to know more.