Skip to:
Content
Pages
Categories
Search
Top
Bottom

HIde BuddyPress and bbPress items from guests

  • @caniwigirl

    Participant

    Last year, @bp-help posted a useful bit of code that prevents non-logged in users from accessing BuddyPress pages… an I believe extended these to forums.

    Unfortunately during all the updates since I got it going a couple weeks ago… this solution no longer seems to work. Any ideas?

    Here is the original code from that post… it goes inside functions.php

    /* Prevent logged out users from accessing bp activity page */
    function nonreg_visitor_redirect() {
    global $bp;
    if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_group_forum() || bp_is_page( BP_MEMBERS_SLUG ) ) {
    if(!is_user_logged_in()) { //just a visitor and not logged in
    wp_redirect( get_option('siteurl') . '/wp-login.php' );
    }
    }
    }
    add_filter('get_header','nonreg_visitor_redirect',1);
Viewing 14 replies - 1 through 14 (of 14 total)
  • @caniwigirl

    Participant

    Forgot to add tags…

    @caniwigirl

    Participant

    Don’t know why but… It is working again. Perhaps it was the latest update?

    Here is another version of the code that can go into the functions.php that I first stumbled on… and is the one I am currently using.

    function bp_guest_redirect() {
    global $bp;
    if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_page( BP_MEMBERS_SLUG ) ) {
    // enter the slug or component conditional here
    if(!is_user_logged_in()) { // not logged in user
    wp_redirect( get_option('siteurl') . '/wp-login.php/' );
    } // user will be redirect to any link to want
    }
    }
    add_filter('get_header','bp_guest_redirect',1);

    Also, I have set my base category for forums to ‘Private’ so that people must be logged in to get access to the forums under that category, but it still allows me to make those forums ‘Public’. The reason? I don’t want members to have to wait for moderators to approve their request to join each of our discussion forums.

    @shanebp

    Moderator

    I’m surprised that it works via add_filter.
    The hook is a do_action.
    And – You don’t need the global.
    And – bp_is_page is deprecated.

    function bp_guest_redirect( $name ) {
    
        if( ! is_user_logged_in() ) {
    
            if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_directory() || bp_is_user() ) 
                wp_redirect( get_option('siteurl') . '/wp-login.php/' );
    
        }
    
    }
    add_action( 'get_header', 'bp_guest_redirect', 1, 1 );

    @bphelp

    Participant

    Yeah you don’t need the global and I would also change:

     
    bp_is_page( BP_MEMBERS_SLUG )
    

    to

    
    bp_is_members_component()
    

    @caniwigirl

    Participant

    Thanks. So, to summarize for others wanting to do the same… The code to add to the functions.php file in the theme (or child theme) would be:

    function bp_guest_redirect( $name ) {
    
        if( ! is_user_logged_in() ) {
    
            if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_directory() || bp_is_user() || bp_is_members_component() ) 
                wp_redirect( get_option('siteurl') . '/wp-login.php/' );
    
        }
    
    }
    add_action( 'get_header', 'bp_guest_redirect', 1, 1 );

    @bphelp

    Participant

    @caniwigirl
    I personally would choose to add it to bp-custom.php this is the best approach in case you decide to change themes down the road because if you place it in your themes functions.php or in a child theme of that themes functions.php then you will loose that functionality if you do change themes. The bp-custom.php acts more or less as a plugin independent from the theme you may use. Good luck!

    @caniwigirl

    Participant

    Awesome, Thanks!

    @minglonaire

    Participant

    Hi, this is amazing, I have been looking for something like this for a long time.
    How would I block forums for guests as well however?

    I added this code… || bp_is_forums_component()

    but forums are still visible to non-logged in users, any ideas?
    Thanks!

    @caniwigirl

    Participant

    Hi @minglonaire,

    The forum part is easy because that is built into bbPress in the first place. If you read the “Getting Started with bbPress” page https://codex.bbpress.org/getting-started-with-bbpress/ you will note that there are three settings for forum visibility:

    Public – Anyone can see these forums
    Private – Only logged in registered users can see these forums
    Hidden: Only Moderators/Admins can see these forums

    I made my base forum (which is actually a category) Private, and the majority of sub-forums Public – but tied into the BuddyPress groups so you required membership to each particular group to access each of the forums. This only works if BuddyPress is hidden from logged out users.

    If you don’t want to use BuddyPress groups… just make all your forums Private.

    Note that BuddyPress defines Public, Private and Hidden slightly differently than bbPress

    I had issues with my theme not displaying Hidden Groups to the people it should… If this is an issue for you, check out another thread… https://buddypress.org/support/topic/hidden-groups-and-forums-not-showing-for-members-of-them/

    All the best! 🙂

    @minglonaire

    Participant

    ah I created forums through groups I have to edit them now to change that.
    Thanks again!

    @caniwigirl

    Participant

    No worries. Editing them from the back end is the quickest way to do it.

    @testla01

    Participant

    Hallo,

    when i try your snipped @caniwigirl with the Theme Accelerate Pro i got following error-message:

    “Warning: Cannot modify header information – headers already sent by (output started at /www/**/wp-content/themes/accelerate-pro/page.php:11) in /www/htdocs/**/wp-includes/pluggable.php on line 1196”

    Do you know what’s wrong?

    thank you

    @caniwigirl

    Participant

    Hi @testla01,

    Just clarifying that you added the last snippet of code I posted into a file called bp-custom.php?

    Sorry, I can’t help you. Perhaps @bphelp or @shanebp can?

    @testla01

    Participant

    hi,

    yes i tried these two versions i found:

    function bp_guest_redirect( $name ) {
    
        if( ! is_user_logged_in() ) {
    
            if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_directory() || bp_is_user() || bp_is_members_component() ) 
                wp_redirect( get_option('siteurl') . '/wp-login.php/' );
    
        }
    
    }
    add_action( 'get_header', 'bp_guest_redirect', 1, 1 );
    <?php
    function restrict_bp_pages() {
    	
    	global $bp;
    	
    	if( bp_is_blog_page() || bp_is_register_page() || bp_is_activation_page() ) {
    		return;
    	}
    	
    	if( !is_user_logged_in() ) {
    		bp_core_redirect( $bp->root_domain .'/YOUR-LOGIN-SLUG' );
    	}	
    	
    }
    add_action( 'get_header', 'restrict_bp_pages' );

    Always i am getting a problem with my pluggable.php??

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘HIde BuddyPress and bbPress items from guests’ is closed to new replies.
Skip to toolbar