Skip to:
Content
Pages
Categories
Search
Top
Bottom

Hiding bbPress topics from logged out users

  • @philipjohn

    Participant

    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 12 replies - 1 through 12 (of 12 total)
  • @whiterau

    Member

    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

    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

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

    @gloomis120

    Member

    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.

    @seancojr

    Member

    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

    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

    @drsammy

    Member

    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.

    @zopfan

    Participant

    Hi, I’m very late to this thread.

    But is there some plugin which could show “You need to login to view this page” instead of “Page cannot be found”?

    @arutam

    Participant

    Hi,


    @philipjohn

    Try this code in your function file, it shows only the titles (of topics) but no the content.

    //Restrict Topic and Replies Content For No-Registered Users
    function pj_hla_logged_in_topics($have_posts){
    if (!is_user_logged_in()){
    $have_posts = null;
    }
    return $have_posts;
    }
    add_filter('bbp_has_replies', 'pj_hla_logged_in_topics');

    Regards,

    @zopfan

    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

    This above link is not working, can you pls share the correct link to that post of yours?

    @robin-w

    Moderator

    There is a plugin that does private groups – hiding anything and everything as needed, with controls on who can see and who can post and when.

    Private groups

    @robin-w

    Moderator

    @zopfan – this code may be useful

    //redirect user to log in page if accessing forum from external link
    add_action( 'template_redirect', 'forum_redirect_from_external_link' );
    function forum_redirect_from_external_link() {
      $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
      $actual_link = explode('/',$actual_link);
      if($actual_link[3]=="forums" && !is_user_logged_in() ) {
        wp_redirect( home_url('your-forum-log-in-page-here') ); 
          exit;
      }
    }
Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.
Skip to toolbar