Skip to content

Instantly share code, notes, and snippets.

@messaoudi-mounir
Last active December 20, 2015 00:39
Show Gist options
  • Save messaoudi-mounir/6043674 to your computer and use it in GitHub Desktop.
Save messaoudi-mounir/6043674 to your computer and use it in GitHub Desktop.
This script exclude Admins users from Members directory on BuddyPress.
add_action( 'bp_legacy_theme_ajax_querystring','bprm_exclude_members',20 , 2 );
add_action( 'bp_ajax_querystring','bprm_exclude_members',20 , 2 );
function bprm_exclude_members( $qs=false, $object = false ){
global $bp;
//list of users to exclude
//comma separated ids of users whom you want to exclude
// for example '0,1,54'
$excluded_members = join( ',', bprm_get_admin_users_ids() );
//if the current loggedin member is in the excluded members
//then the exclusion is ignored
if ( bp_loggedin_user_domain() ) {
$user_id = $bp->loggedin_user->id;
if ( in_array( $user_id , explode( ",", $excluded_members ) ) )
return $qs;
}
if( $object != 'members' )//hide for members only
return $qs;
$args = wp_parse_args($qs);
//check if we are searching for friends list etc?, do not exclude in this case
if(!empty($args['user_id']))
return $qs;
if(!empty($args['exclude']))
$args['exclude'] = $args['exclude'].','. $excluded_members;
else
$args['exclude'] = $excluded_members;
$qs = build_query($args);
return $qs;
}
add_filter( 'bp_core_get_user_domain', 'bprm_core_get_user_domain' ,10 , 4 );
function bprm_core_get_user_domain($domain, $user_id, $user_nicename, $user_login){
global $bp;
//list of users to exclude
// for example array(1,2,55)
$excluded_members = bprm_get_admin_users_ids();
//if the current loggedin member is in the excluded members
//then the exclusion is ignored
if ( is_user_logged_in() ) {
$loggedin_user_id = $bp->loggedin_user->id;
if ( in_array( $loggedin_user_id , $excluded_members ) )
return $domain;
}
if ( in_array( $user_id , $excluded_members ) )
return null;
}
function bprm_get_admin_users_ids(){
$admins = get_users( array( 'role' => 'administrator' ,'fields'=>'ID') );
return $admins;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment