This little code snippet will protect all BuddyPress & bbPress pages and force users to go to Register page before accessing them
So if a user, that doesn’t have an account, visits your BuddyPress Members page, an user Profile page, groups, a Group page, activity page or any other bbPress forums pages will get redirected to the Registration page.
It is a good idea to use it if you want to force users to register before seeing other members or other social functionality.
Just add this code to your functions.php file in the child theme and the magic will happen.
/** * Redirect buddypress and bbpress pages to registration page */ function kleo_page_template_redirect() { //if not logged in and on a bp page except registration or activation if( ! is_user_logged_in() && ( ( ! bp_is_blog_page() && ! bp_is_activation_page() && ! bp_is_register_page() ) || is_bbpress() ) ) { wp_redirect( home_url( '/register/' ) ); exit(); } } add_action( 'template_redirect', 'kleo_page_template_redirect' );
If you want to restrict only BuddyPress pages from guest users then use this code snippet:
/** * Redirect buddypress pages to registration page */ function kleo_page_template_redirect() { //if not logged in and on a bp page except registration or activation if( ! is_user_logged_in() && ! bp_is_blog_page() && ! bp_is_activation_page() && ! bp_is_register_page() ) { wp_redirect( home_url( '/register/' ) ); exit(); } } add_action( 'template_redirect', 'kleo_page_template_redirect' );
If you want to restrict only bbPress pages from guest users then use this code snippet:
/** * Redirect bbPress pages to registration page */ function kleo_page_template_redirect() { //if not logged in and on a bp page except registration or activation if( ! is_user_logged_in() && is_bbpress() ) { wp_redirect( home_url( '/register/' ) ); exit(); } } add_action( 'template_redirect', 'kleo_page_template_redirect' );
People reacted to this story.
This is great functionality – just wondering why you wouldn’t include it in the next release of the Kleo theme?
Cheers,
Sven
spacehacker.com
Thanks, we will probably put it also in Kleo but since it is just a small code it can be easily added to your child theme functions.php
Can this code be altered to not include the BB Forum so only the buddypress content forces guests to register?
Yes I will updated it today 🙂
Awesome – let me know when I can grab it!
I already updated the post. Check it out 😉
Have a nice day.
Cheers – saw that about 2 seconds after replying – works perfect and getting a lot more registrations as a result
Thanks for this post! Just saved me quite a headache solving exactly the issue of accessibility of BBPress and BuddyPress sites 🙂
glad to hear that 🙂
Awesome.. thanx.. 🙂
Hey.
It is stated that this code should be placed in the child theme? I am using the xphoria theme. Is that considered a child theme?
Hi, the theme author should provided a child theme to activate so any changes to the theme don’t get lost on update.
great! thank you!
Could u give me a hint on how to correctly rename the link to the theme in the code?
Erik I don’t understand what you mean.. The code above should go to you theme functions.php
Hi there,
I wonder if it’s possible to redirect users back to the page they were trying to access at first before they were redirected to the login form.
So this is for a use case in which the users receive a link to restricted content in their email and the website will ask them to login before proceeding to the requested page.
Can this be done extending the snippet above?
Not an easy request but you can try passing the existing url to your login page as a parameter somehow..
Users usually find a redirect to a registration page quite harsh and it does not always make sense.
A better approach would be to kick in the modal login instead.
If the viewer is already a registered user, then why redirecting him to the registration page?
If the viewer is not yet a registered user, then he gets the message that he needs to register and click himself on the registration link on the modal login.
How to trigger the modal login to access restricted content instead?
Hi, that can’t be done like that since you need to hook into the link generation and this hooks on the template redirect.
Hello, I need a code for restrict URL’s on bbPress forums from not registered users. can you help for this?
Hi, I will add in a couple of minute the code just for bbPress. Cheers
Hi, thanks for your reply. did you add code somewhere i couldnt find.
Hi, i tried your bbpress code. But i need to restrict only urls for non registered users. Can you suggest a plugin or a code to fix that?
For example > a non loged in users will see for http://seventhqueen.com/ > please log in to see content
Thanks for your help
Thank you so much for the code snippet, I searched all over for this solution and all of the plugins are outdated or not suitable for our use.
We are using Multisite so have your snippet in bp-custom. Is there a way to exclude the sub blogs from redirecting? We need non logged in users to be able to see that page.
Again, thank you for this bit of code, we really didn’t want to go with any solutions such as a membership plugin for this
Just to add a solution to a multisite problem, on multisite (without Domain Mapping) when a user visits a subsite they are automatically logged in to the site. This posed a problem for us since we are educational and didn’t want everyone seeing member information for a subsite of which they were not registered. (Privacy) From your snippet above we added to it to accomplish the goal of keeping subsites visible but keeping BP information private.
We ran across a bit of code we were not aware of that did the trick as you will see in the 2nd function ( is_user_member_of_blog ) The walled garden function was something laying around in our files but explainable with a Google search! BTW, this is placed in our bp-custom file.
Here it goes!
function kleo_page_template_redirect()
{
//if not logged in and on a bp page except registration or activation
if( ! is_user_logged_in() && ! bp_is_blog_page() && ! bp_is_activation_page() && ! bp_is_register_page() && ! bp_is_blogs_component() ) {
wp_redirect( home_url( ‘/register/’ ) );
exit();
}
}
add_action( ‘template_redirect’, ‘kleo_page_template_redirect’ );
// hide from network logged in but non member of stie
function sh_walled_garden()
{
if( ! is_user_member_of_blog() && ! bp_is_blog_page() && ! bp_is_activation_page() && ! bp_is_register_page() && ! bp_is_blogs_component() ) {
wp_redirect( home_url( ‘/register/’ ) );
exit();
}
}
add_action( ‘bp_init’, ‘sh_walled_garden’ );
For us, this code is used with the plugin BP Multi Network (https://wordpress.org/plugins/bp-multi-network/)
and define ( ‘BP_ENABLE_MULTIBLOG’, true ); in wp-config
When a user registers on a subsite they are NOT automatically added to the main site in our experience. Yours may vary!
For multisite users, this has been a “bane of existance” so I hope this combination helps solve the issue for many and THANK YOU SeventhQueen for you inspiration.
many thanks for sharing the code. All the best
Never mind I found it
&& ! bp_is_blogs_component()
Great 🙂
I pasted the code in the Kleo Child theme functions.php file, but it won’t update. It keeps returning a 404 page. Any suggestions? Thank you.
Hi, well you should check where you are redirected in the browser and adjust the code to match your needs
Please, I get the following error message:
Fatal error: Call to undefined function is_bbpress() in /home/xxxx/public_html/xxx.com/wp-content/themes/kleo-child/functions.php on line 20
What is wrong?
Thanks
that is because bbPress is not installed. Use the other code just for Buddypress
Hi, did this privacy/members only code go into the theme -I can’t find it?
Hi, I can’t understand what you need..
Neither did I when I read it back 🙂 What I meant to say was that there was a mention amongst these comments that you may include this code in future kleo releases. I was wondering if that happened and if so whether there was an option to check in order to restrict guest users from accessing the buddypress components of kleo themes.
Nope, not included it for the moment
Can we somehow make Members Page and Blog Page only for registered users?
Hi, I think is best to use a plugin for restriction in this case. you need to check if the user is on a specific page in the above logic
I used it on my buddyapp theme and I ended up with the white screen of death…any suggestions?
You should check how you added the code and also check for errors on your hosting
Hi
Could you help me by adding a version that allows non logged in users to also access the group and member directories as well? Only if they try and then click a member or group to view the buddpress page do I want them kicked to to the registration page.
Many thanks
Hi, that is a bit custom and it isn’t a quick code snippet.
Cheers
..for the Kleo Theme, would be great to show the login/register popup for non logged in viewers, that my convert the one or other visitor into a registered user, what do you think?
Hi 7queen,
I am using sweetdate theme, I tried all the above one by one, none that works? I still able to click on the members page, activity, member profile, etc?
Any idea why?
Please advise help,
Thanks
How I cam restrict all pages includes (homepage,wp all pages)
[…] found ways to do this via plugins (e.g. BuddyPress Members Only) or tweaks (e.g. here and here), but nothing quite fit my needs. The plugins were mostly all or nothing, and the tweaks were […]
Thanks! This was a help!
[…] found ways to do this via plugins (e.g. BuddyPress Members Only) or tweaks (e.g. here and here), but nothing quite fit my needs. The plugins were mostly all or nothing, and the tweaks were […]
Thanks for the code snippet! Of course, it works with any theme (like charm). I have been testing various plugins that could do this very basic job and all has the bells and whistles but cannot perform such a task (so I myself decided to do something with a code snippet but as I am not a coder myself, you spared me a ton of experimenting through trial and error).
Anyway, thanks again!
Thank you Connie!! I wish I had known you then, so I could have invited you haha
How to redirect /member/me/ to custom register page?
as we know /members/me/ is the dynamic link for an autodetected username which is BP page, but this code cant cover it 🙁
[…] Resources: (BuddyPress Slack channel) 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.… […]
Hi,
Is there any possibility to restrict moderators and even Keymasters to see specific forums and categories that has multiple child elements?
Thanks