Restrict not logged in users from accessing BuddyPress or bbPress pages

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' );
We are SeventhQueen – a crazy and enthusiastic team with unique ideas that have already charmed many great customers.
42 Comments
  1. Sven 2 years ago

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

    Cheers,

    Sven

    spacehacker.com

  2. Author
    sqadmin 2 years ago

    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

  3. Peter 2 years ago

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

    • Author
      SeventhQueen 2 years ago

      Yes I will updated it today 🙂

      • Peter Gould 2 years ago

        Awesome – let me know when I can grab it!

        • Author
          SeventhQueen 2 years ago

          I already updated the post. Check it out 😉

          Have a nice day.

          • Peter Gould 2 years ago

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

  4. Philip 2 years ago

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

    • Author
      SeventhQueen 2 years ago

      glad to hear that 🙂

  5. Swati 2 years ago

    Awesome.. thanx.. 🙂

  6. Erik 2 years ago

    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?

    • Author
      SeventhQueen 2 years ago

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

  7. Erik 2 years ago

    great! thank you!

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

    • Author
      SeventhQueen 2 years ago

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

  8. timo 2 years ago

    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?

    • Author
      SeventhQueen 1 year ago

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

  9. Mel 1 year ago

    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?

    • Author
      SeventhQueen 1 year ago

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

  10. Gürsel 1 year ago

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

    • Author
      SeventhQueen 1 year ago

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

      • Gürsel 1 year ago

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

      • Gürsel 1 year ago

        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

  11. Jeffrey 1 year ago

    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

    • Jeffrey 1 year ago

      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.

      • Author
        SeventhQueen 1 year ago

        many thanks for sharing the code. All the best

  12. Jeffrey 1 year ago

    Never mind I found it

    && ! bp_is_blogs_component()

    • Author
      SeventhQueen 1 year ago

      Great 🙂

  13. Scott 1 year ago

    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.

    • Author
      SeventhQueen 1 year ago

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

  14. Elio 1 year ago

    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

    • Author
      SeventhQueen 1 year ago

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

  15. Investa 11 months ago

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

    • Author
      SeventhQueen 11 months ago

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

  16. jo 11 months ago

    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.

    • Author
      SeventhQueen 11 months ago

      Nope, not included it for the moment

  17. Niko 10 months ago

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

    • Author
      SeventhQueen 10 months ago

      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

  18. Biruk 9 months ago

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

    • Author
      SeventhQueen 8 months ago

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

  19. Robin 6 months ago

    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

    • Author
      SeventhQueen 5 months ago

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

      Cheers

  20. Eric 3 months ago

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

Leave a reply

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

*

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

©2016 SeventhQueen - Creative Premium Wordpress Themes | Website Templates

or

Log in with your credentials

Forgot your details?