how to hide bp members page from site guests

I am trying to hide the Buddypress members page from anyone not logged in. We run a multisite network and all site have buddypress installed and all use the same theme. I was hoping to just add a snippet into the functions.php file. I found this code on a 3 year old thread but it doesn't seem to have any effect: `function bp_guest_redirect() {
global $bp;
if ( bp_is_activity_component() || bp_is_groups_component() /*|| bbp_is_single_forum()*/ || bp_is_forums_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’) . ‘/register’ );
} // user will be redirect to any link to want
}
}
add_filter(‘get_header’,’bp_guest_redirect’,1);`

  • Rupok
    • Support Ninja

    Hi jono_39

    Hope you had a wonderful day.

    Paste this code in your theme functions.php file for hiding BuddyPress members pages from anyone not logged in.

    function bp_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()) {
    			wp_redirect( get_option('siteurl') . '/wp-login.php' );
    		}
    	}
    }
    add_filter('get_header','bp_visitor_redirect',1);

    But I won't suggest you to do this. Because, if your theme gets updated, or you need to change your theme, you will have to do it again in your new theme. So the best option will be using a MU Plugin. For creating a mu plugin, create a folder named "mu-plugins" inside your "wp-content" folder. Then inside that "mu-plugins" folder, create a file with any name and with a .php extension. Now paste the code inside that file and save.

    <?php
    function bp_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()) {
    			wp_redirect( get_option('siteurl') . '/wp-login.php' );
    		}
    	}
    }
    add_filter('get_header','bp_visitor_redirect',1);

    Code is same like above, just here you need to declare one '<?php' tag at the beginning.

    For getting more idea on mu plugins, you can go through this article: https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    Please let us know if you have any confusion or query. We will be glad to help.

    Have a nice day. Cheers!
    Rupok

  • Ash
    • WordPress Hacker

    Hello @jono_39

    I hope you are doing good.

    Please try the following code:

    add_action( 'template_redirect', 'hide_member_page' );
    function hide_member_page() {
        global $bp;
        if( bp_current_component() == 'members' && ! is_user_logged_in() ) {
            wp_redirect( 'site_url' );
            exit;
        }
    }

    if it doesn't work, then you need to know the member page ID. From Settings > BuddyPress > Pages you have selected a page for members page, get the ID of that page. Then use the following code:

    add_action( 'template_redirect', 'hide_member_page' );
    function hide_member_page() {
        global $post;
        if( $post->ID == 111 ) {
            if( ! is_user_logged_in() ) {
                wp_redirect( 'site_url' );
                exit;
            }
        }
    }

    Replace 111 with the correct ID.

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.