There will be no theme support from 23rd December to January 1st. Support will resume January 2nd.

Okay
  Print

How do I restrict access to the BuddyPress members directory and profile pages?

You will need to be use the Paid Membership Pro plugin to create membership levels, charge for access and restrict specific content. You'll also need to use the BuddyPress Add On to control access to specific BuddyPress content. However this plugin does not allow you to control access to the members directory and profiles by default.

1) To add this functionality in your child theme's functions.php file add:

/**
 * Restrict viewing of the members directory and individual profiles if the user doesn't have access
 *
 */
if ( defined( 'PMPRO_VERSION' ) && defined( 'PMPROBP_DIR' ) && function_exists( 'bp_is_active' ) && ! function_exists( 'ghostpool_pmpro_bp_restrict_member_viewing' ) ) {
    function ghostpool_pmpro_bp_restrict_member_viewing() {
        if ( ! function_exists( 'pmpro_getMembershipLevelForUser' ) ) {
            return;
        }
    
        if ( ! is_buddypress() ) {
            return;
        }
        
        global $current_user;
        $user_id = $current_user->ID;
    
        if ( ! empty( $user_id ) ) {
            $level = pmpro_getMembershipLevelForUser( $user_id );
        }
        if ( ! empty( $level ) ) {
            $level_id = $level->id;
        } else {
            $level_id = 0;    // non-member user
        }
        if ( ! bp_is_user() && bp_is_members_component() && $level_id === 0 ) {        
            //pmpro_bp_redirect_to_access_required_page();
        }
        if ( bp_is_user() && $level_id === 0 ) {
            pmpro_bp_redirect_to_access_required_page();
        }
    
    }
    add_action( 'template_redirect', 'ghostpool_pmpro_bp_restrict_member_viewing' );
}

3) You can control what membership levels can access the members directory by editing the following code:

if ( ! bp_is_user() && bp_is_members_component() && $level_id === 0 ) {        
    //pmpro_bp_redirect_to_access_required_page();
}

Change 0 to whatever level cannot access the members directory. 0 refers to members without any level. Uncomment "pmpro_bp_redirect_to_access_required_page();" to enable the restriction.

4) You can control what membership levels can access the member profiles by editing the following code:

if ( bp_is_user() && $level_id === 0 ) {            
    pmpro_bp_redirect_to_access_required_page();        
}    

Change 0 to whatever level cannot access the members directory. 0 refers to members without any level. Comment "pmpro_bp_redirect_to_access_required_page();" to disable the restriction.