Hiding bbPress topics from logged out users

I just spent hours figuring this out so I thought I’d try and save others the bother by sharing it here.

The basic aim was to hide the contents of topics in a bbPress 2.0 forum from any users not logged in. This makes for a nice private forum.

I found suggestions about making forums hidden, private and so on but none of them really worked as they hid them for logged in users too.

My final solution was to make bbPress ‘dumb’ when it comes to logged out users. I.e., it could either be clever and say “there are topics, but you’re not allowed to see them until you login” or it could just say “there are no topics”.

The following code does the latter, dumb version by hooking into the forums, topics and replies loops.

function pj_hla_logged_in_topics($have_posts){
if (!is_user_logged_in()){
$have_posts = null;
}
return $have_posts;
}
add_filter('bbp_has_topics', 'pj_hla_logged_in_topics');
add_filter('bbp_has_forums', 'pj_hla_logged_in_topics');
add_filter('bbp_has_replies', 'pj_hla_logged_in_topics');

I simply placed this within my theme’s functions.php but you could just as easily wrap this in a plugin.


Posted

in

by

Comments

12 responses to “Hiding bbPress topics from logged out users”

  1. Spence Avatar

    Hi John!

    Awesome workaround!

    We’ve taken it a little further and added a warning dialogue box to let users know that they have to log-in to see the forums. Otherwise the default message may be slightly misleading.

    (Hope this formats well in comments)

    Cheers!
    Spence

    /*———————————————————————————–*/
    /* Hide bbPress topics from viewers not logged-in */
    /*———————————————————————————–*/

    function lab_logged_in_bbptopics($have_posts){
    if (!is_user_logged_in()){
    $log_in = ‘Sorry, you must be logged-in to view the forums!’;
    $have_posts = null;

    }
    echo $log_in;
    return $have_posts;
    }
    add_filter(‘bbp_has_topics’, ‘lab_logged_in_bbptopics’);
    add_filter(‘bbp_has_forums’, ‘lab_logged_in_bbptopics’);
    add_filter(‘bbp_has_replies’, ‘lab_logged_in_bbptopics’);

    1. Spence Avatar

      Pardon… I meant to type “Hi Philip” rather than “John” … 😉

    2. Philip John Avatar

      Awesome, didn’t realise it’d be that easy – thanks for sharing!

  2. Kaitlin Avatar
    Kaitlin

    Hi Philip, this is exactly what I need, I’m also trying to hide my bbPress forum from non members.
    However when I paste the code into my theme’s functions.php, my whole WP site disappears! I’m left with a blank screen.
    Any idea why?
    Thanks in advance,
    Kaitlin

  3. Marie Avatar

    Awesome! I’ve been looking for this functionality (…kind-a-sort-a… =), so now I can hopefully move on to next step!

  4. anamika Avatar
    anamika

    Hi Philip, I was looking such kind of stuff for logged out users.

    However when I paste the code into my theme’s functions.php, It is showing ‘Oh bother! No forums were found here!’ message when I’m logged in with admin. And after this message no forum, topic is displaying.

    Any idea about it?

    Thanks in advance,
    Anamika

    1. philipjohn Avatar
      philipjohn

      Anamika, I suggest you try the following;
      – Make sure you are logged in. Log out and back in again to refresh your cookies.
      – Check that the code you have pasted is exactly the same as the code I’ve provided above.
      – Double check you’re looking at a forum with topics already.

  5. Danni Avatar

    Hi, Mine keeps showing the wrong message –> ‘Oh bother! No forums were found here!’ also. I do have forums and topics.
    Any ideas? 🙂
    Thank you

    1. philipjohn Avatar
      philipjohn

      Will take a look for you Danni – can you tell me what versions of WordPress and bbPress you are using please?

  6. Katarina Avatar
    Katarina

    Hi,

    is this still working? When I tried to insert the code into my TwentyFourteen code the whole page broke and I had to restore the functions file.
    Thank you,
    Katarina

    1. philipjohn Avatar
      philipjohn

      Katarina, this worked with a very old version of WordPress and bbPress so you may need something more to to date.

  7. Javier Avatar

    Hey Philip!

    Thanks for this great code! God damn!

    You deserve a Medal!

    I searched this code for long time ago…

    Just to tell you and Katarina the code works for me, I am using WP 4.3.3 and bbPress 2.5.8

    Regards

Leave a Reply

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