Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Hiding self in members directory displays admins


  • navyspitfire
    Participant

    @navyspitfire

    I am trying to hide the currently logged in members as well as admins from the members directory/search results. I’m using the following code, but everything I try and hide the currently logged in user ($excluded_self=bp_loggedin_user_id();), the admins reappear; commenting out the aforementioned line hides the admins. Does anyone know why that is and how to fix it?

    //hide all subscribers and admins from search results area
    add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
    function bpdev_exclude_users($qs=false,$object=false){
        //list of users to exclude
         
        if($object!='members')//hide for members only
            return $qs;
            
        $excluded_user = implode(',',get_users('role=subscriber&fields=ID'));
        $excluded_admins = implode(',',get_users('role=administrator&fields=ID'));
        //$excluded_self=bp_loggedin_user_id();
        
        $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_user;
            $args['exclude']=$args['exclude'].','.$excluded_admins;
            //$args['exclude']=$args['exclude'].','.$excluded_self;
        } else {
            $args['exclude']=$excluded_user;
            $args['exclude']=$excluded_admins;
            //$args['exclude']=$excluded_self;
        }
          
        $qs=build_query($args);
       
       return $qs;  
    }

    A different issue: the number of resulting members returned when a user searches still includes admins/removed users&roles. Is there a way to fix the results count to exclude those? Can I modify the code below for that?

    function bpfr_hide_get_total_filter($count){
        return $count-1;
    }
    add_filter('bp_get_total_member_count','bpfr_hide_get_total_filter');
Viewing 2 replies - 1 through 2 (of 2 total)

  • danbp
    Participant

    @danbp

    Hi @navyspitfire,

    when you pick up code somewhere, you should also read the comments. The solution to your issue was in one of them.

    Here is what wil exclude site admin from members directory and exclude also the logged_in user. The total member count will also be adjusted correctly on All members [x] tab and for the pagination count. $count-2 instead of $count-1 do the trick.

    Excluded members, incl. admin, are of course excluded from search.

    function bpex_hide_admin_on_member_directory( $qs=false, $object=false ){
    
    	// Id's to hide, separated by comma
    	$excluded_user = '1' ;
    
    	// hide to members & friends 
    	if($object != 'members' && $object != 'friends')
    	return $qs;
    	
    	$args = wp_parse_args($qs);
    	
    	if(!empty($args['user_id']))
    	return $qs;	
    	
    	if(!empty($args['exclude']))
    	$args['exclude'] = $args['exclude'].','.$excluded_user;
    	else
    	$args['exclude'] = $excluded_user;
    	
    	$qs = build_query($args);
    	
    	return $qs;
    	
    }
    add_action( 'bp_ajax_querystring','bpex_hide_admin_on_member_directory', 20, 2 );
    
    // once admin is excluded, we must recount the members !
    function bpex_hide_get_total_filter( $count ){
    	return $count-2;
    }
    add_filter( 'bp_get_total_member_count', 'bpex_hide_get_total_filter' );
    
    function bpex_exclude_loggedin_user( $qs = false, $object = false ) {
    
     //list of users to exclude
     if( !is_user_logged_in() )
         return $qs;
    
     //if the user is logged in , let us exclude her/him
     $excluded_user=  get_current_user_id();
      
     if( $object !='members' )//hide for members only
    	return $qs;
      
     $args=wp_parse_args($qs);
      
     //check if we are listing friends?, do not exclude in this case
     if( !empty( $args[ 'user_id' ] ) )
    	return $qs;
      
     if( !empty( $args['exclude'] ) )
    	$args['exclude'] = $args['exclude'] .','. $excluded_user;
     else
    	$args['exclude'] = $excluded_user;
      
    	$qs = build_query( $args );
      
     return $qs;
      
    }
    add_action('bp_ajax_querystring','bpex_exclude_loggedin_user', 20, 2 );

    Codex Reference

    Playing with the user’s ID in different contexts


    navyspitfire
    Participant

    @navyspitfire

    Hi Dan, thank you for your snippet. My issue was that I was trying to hide subscribers, admins, and the current user in the same function; separating subscribers/admins and current user into two different functions did the trick.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Hiding self in members directory displays admins’ is closed to new replies.
Skip to toolbar