• Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

Easy Web Design Tutorials

WordPress Tutorials and more

  • Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

21 October - 2018 By Paal Joachim 6 Comments
Last updated on: October 23, 2018

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.

 

BuddyPress-bbPress-redirect-not-logged-in-users

 

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

Share this:

  • Email

Categories: Plugins, WordPress

Paal Joachim Romdahl

I enjoy teaching and creating tutorials. As well creating web sites.
I help people gain WordPress knowledge through my easy to follow tutorials and specialized training. Contact me for more information on how I can improve your WordPress skills and to help get your web site quickly up and running.

Comments

  1. Nikola Petrov says

    9 January - 2021

    Redirect private bbPress forum to specific page works perfectly nice!
    Thank you very much for this!

    Reply
  2. bullet force says

    11 May - 2020

    Thank you for your support of this extremely complex code problem, the information is extremely helpful!

    Reply
  3. English teacher says

    2 May - 2020

    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.

    Reply
  4. Yannic says

    22 February - 2020

    Thanks a lot! This code blocks access to the registration form as well, do you know how to keep the form public?

    Reply
  5. Oil and Gas Inspection says

    18 December - 2018

    Hello
    Thank you for your site
    I had a problem with the page builder wpbakery of eternity
    Thank you for helping me

    Reply
  6. آموزش متره و برآورد says

    21 November - 2018

    thank you so much. hope you help people like me to know more.

    Reply

Leave a Comment Cancel reply

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.

© 2021 · By Easy Web Design Tutorials · Built on the Genesis Framework · WordPress · Log in · ⇪

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.