redirect-buddypress-to-register-pageThis 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' );
Do you like SeventhQueen's articles? Follow on social!
People reacted to this story.
Show comments Hide comments
Comments to: Restrict not logged in users from accessing BuddyPress or bbPress pages
  • March 27, 2015

    This is great functionality – just wondering why you wouldn’t include it in the next release of the Kleo theme?

    Cheers,

    Sven

    spacehacker.com

    Reply
  • March 27, 2015

    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

    Reply
  • March 31, 2015

    Can this code be altered to not include the BB Forum so only the buddypress content forces guests to register?

    Reply
    • April 1, 2015

      Yes I will updated it today 🙂

      Reply
      • April 2, 2015

        Awesome – let me know when I can grab it!

        Reply
        • April 2, 2015

          I already updated the post. Check it out 😉

          Have a nice day.

          Reply
          • April 7, 2015

            Cheers – saw that about 2 seconds after replying – works perfect and getting a lot more registrations as a result

  • April 21, 2015

    Thanks for this post! Just saved me quite a headache solving exactly the issue of accessibility of BBPress and BuddyPress sites 🙂

    Reply
    • April 21, 2015

      glad to hear that 🙂

      Reply
  • May 8, 2015

    Awesome.. thanx.. 🙂

    Reply
  • May 25, 2015

    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?

    Reply
    • May 25, 2015

      Hi, the theme author should provided a child theme to activate so any changes to the theme don’t get lost on update.

      Reply
  • May 25, 2015

    great! thank you!

    Could u give me a hint on how to correctly rename the link to the theme in the code?

    Reply
    • May 29, 2015

      Erik I don’t understand what you mean.. The code above should go to you theme functions.php

      Reply
  • May 28, 2015

    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?

    Reply
    • June 7, 2015

      Not an easy request but you can try passing the existing url to your login page as a parameter somehow..

      Reply
  • July 22, 2015

    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?

    Reply
    • August 20, 2015

      Hi, that can’t be done like that since you need to hook into the link generation and this hooks on the template redirect.

      Reply
  • August 9, 2015

    Hello, I need a code for restrict URL’s on bbPress forums from not registered users. can you help for this?

    Reply
    • August 20, 2015

      Hi, I will add in a couple of minute the code just for bbPress. Cheers

      Reply
      • August 21, 2015

        Hi, thanks for your reply. did you add code somewhere i couldnt find.

        Reply
      • August 25, 2015

        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

        Reply
  • August 23, 2015

    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

    Reply
    • September 3, 2015

      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.

      Reply
      • September 9, 2015

        many thanks for sharing the code. All the best

        Reply
  • August 23, 2015

    Never mind I found it

    && ! bp_is_blogs_component()

    Reply
    • August 23, 2015

      Great 🙂

      Reply
  • September 13, 2015

    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.

    Reply
    • September 16, 2015

      Hi, well you should check where you are redirected in the browser and adjust the code to match your needs

      Reply
  • September 29, 2015

    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

    Reply
    • September 30, 2015

      that is because bbPress is not installed. Use the other code just for Buddypress

      Reply
  • January 3, 2016

    Hi, did this privacy/members only code go into the theme -I can’t find it?

    Reply
    • January 3, 2016

      Hi, I can’t understand what you need..

      Reply
  • January 4, 2016

    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.

    Reply
    • January 9, 2016

      Nope, not included it for the moment

      Reply
  • January 24, 2016

    Can we somehow make Members Page and Blog Page only for registered users?

    Reply
    • January 31, 2016

      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

      Reply
  • March 18, 2016

    I used it on my buddyapp theme and I ended up with the white screen of death…any suggestions?

    Reply
    • March 25, 2016

      You should check how you added the code and also check for errors on your hosting

      Reply
  • June 20, 2016

    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

    Reply
    • June 25, 2016

      Hi, that is a bit custom and it isn’t a quick code snippet.

      Cheers

      Reply
  • August 30, 2016

    ..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?

    Reply
  • September 15, 2016

    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

    Reply
  • December 21, 2016

    How I cam restrict all pages includes (homepage,wp all pages)

    Reply
  • […] 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 […]

    Reply
  • January 5, 2017

    Thanks! This was a help!

    Reply
  • […] found ways to do this via plu­gins (e.g. BuddyPress Mem­bers Only) or tweaks (e.g. here and here), but noth­ing quite fit my needs. The plu­gins were mostly all or noth­ing, and the tweaks were […]

    Reply
  • June 22, 2018

    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!

    Reply
  • Thank you Connie!! I wish I had known you then, so I could have invited you haha

    Reply
  • October 13, 2018

    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 🙁

    Reply
  • […] 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.… […]

    Reply
  • April 30, 2019

    Follow This link. Just a simple snippet.

    https://vvcares.com/w/wordpress-hide-pages-for-non-logged-in-users/

    The snippet will do:
    IF the user is not logged-in..
    AND the page name is ‘members’ OR ‘activity’ OR bp-profile-page..
    THEN it will redirect to REGISTER page.

    Reply
Write a response

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.