Skip to:
Content
Pages
Categories
Search
Top
Bottom

Hiding bbPress topics from logged out users


  • philipjohn
    Participant

    @philipjohn

    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.

    Code:
    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 function.

    Hope that helps someone!

Viewing 7 replies - 1 through 7 (of 7 total)
  • niiiice. you could also check out s2Member plugin, but this is just plain slick.

    i’m sure we’ll be seeing an update soon that will asplode everything we’ve done so far, but until then, very cool workaround.

    WR!


    gloomis120
    Member

    @gloomis120

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

    Thanks for the code! I have it working but I dont follow what you mean with the quoted login.

    Is this a shortcode setting, or are you changing the default message of “Oh bother! No forums were found here!”.


    philipjohn
    Participant

    @philipjohn

    I’m not changing the message at all – hence the “dumb” behaviour.


    gloomis120
    Member

    @gloomis120

    I see. You were suggesting that we change the message to something else.

    Thanks for this code. Ive been waiting for something like this, and was about to start working on a plugin to do the same thing.

    I’ve looked into a plugin called Role Scoper which grants you the ability to restrict things based on WordPress user roles and capabilities. Admittedly, the plugin is a bit bulky for what’s being accomplished with the above code but it’s an option.


    philipjohn
    Participant

    @philipjohn

    I blogged about this little tweak too and someone has just posted an edit to display a friendly message instead of the standard “not found” which sort of completes it :)

    http://philipjohn.co.uk/2011/11/14/hiding-bbpress-topics-from-logged-out-users/#comment-1885

    Found Phillips’ brilliant code awhile back. I changed it a little bit for more control:

    function lab_logged_in_bbptopics($have_posts){
    if (!bbp_current_user_can_publish_forums()){
    $have_posts = null;
    }
    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’);
    

    You can then use any of the user role plugins to specify which user (or user level) can publish forums. Of course, logged out users don’t have a bbp_current_user_can_publish_forums() set so they’ll automatically be denied access to the forums. Hope you enjoy!

    Also, I changed the “Oh bother! …” text in bbpress/feedback-no-forums.php to say “You have to be logged in and have permission to see the forums.”

    Hope this helps someone. And, thank you Philip for sharing your code to begin with! It certainly helped me a lot.

Viewing 7 replies - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.