BuddyPress: Hide Administrators From the Members Directory
Keeps site administrators (and any other roles you list) out of the BuddyPress members directory and member counts on the directory page.
Opens WPIDE on
Code
if ( ! function_exists( 'buddypress' ) ) {
return;
}
// Roles to hide from the directory.
$wpide_bp_hidden_roles = array( 'administrator' );
add_filter( 'bp_after_has_members_parse_args', function ( $args ) use ( $wpide_bp_hidden_roles ) {
if ( ! bp_is_members_directory() && ! wp_doing_ajax() ) {
return $args;
}
$hidden = get_transient( 'wpide_bp_hidden_members' );
if ( false === $hidden ) {
$hidden = get_users( array( 'role__in' => $wpide_bp_hidden_roles, 'fields' => 'ID', 'number' => 500 ) );
set_transient( 'wpide_bp_hidden_members', $hidden, HOUR_IN_SECONDS );
}
if ( $hidden ) {
$exclude = empty( $args['exclude'] ) ? array() : wp_parse_id_list( $args['exclude'] );
$args['exclude'] = array_unique( array_merge( $exclude, array_map( 'intval', $hidden ) ) );
}
return $args;
} );
add_action( 'set_user_role', function () {
delete_transient( 'wpide_bp_hidden_members' );
} );
Notes
Hidden members can still be found by visiting their profile URL directly. The list of IDs is cached for an hour.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.