BuddyDev

Search

[Resolved] Exclude admin from all members lists?

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #45621

    Hi there, I’m using this function below to exclude logged and admin users from BuddyPress users list.

    https://buddydev.com/support/forums/topic/hide-current-logged-in-user-and-admin/#post-14503

    But admin is still displayed when using BP widgets like Who’s Online.
    Can I add something to this function to exclude admin from all user lists?

    Regards
    Carsten

    function buddydev_exclude_logged_and_admin_users( $args ) {
    	//do not exclude in admin
    	if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
    		return $args;
    	}
    
    	$excluded = isset( $args['exclude'] ) ? $args['exclude'] : array();
    
    	if ( ! is_array( $excluded ) ) {
    		$excluded = explode( ',', $excluded );
    	}
    
    	$role     = 'administrator';//change to the role to be excluded
    	$user_ids = get_users( array( 'role' => $role, 'fields' => 'ID' ) );
    
    	$excluded = array_merge( $excluded, $user_ids );
    
    	if ( is_user_logged_in() ) {
    		array_push( $excluded, get_current_user_id() );
    	}
    
    	$args['exclude'] = $excluded;
    
    	return $args;
    } 
    
    add_filter( 'bp_after_has_members_parse_args', 'buddydev_exclude_logged_and_admin_users' )
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #45622

    Hello Carsten,

    Thank you for posting. Please try the following code:

    
    
    /**
     * Exclude logged in and admin users
     *
     * @param array $args Member args.
     *
     * @return mixed
     */
    function buddydev_exclude_logged_and_admin_users( $args ) {
    	// Do not exclude in admin.
    	if ( is_admin() && ! wp_doing_ajax() ) {
    		return $args;
    	}
    
    	$excluded = isset( $args['exclude'] ) ? $args['exclude'] : array();
    
    	if ( ! is_array( $excluded ) ) {
    		$excluded = explode( ',', $excluded );
    	}
    
    	$role     = 'administrator';//change to the role to be excluded
    	$user_ids = get_users( array( 'role' => $role, 'fields' => 'ID' ) );
    
    	$excluded = array_merge( $excluded, $user_ids );
    
    	if ( is_user_logged_in() ) {
    		array_push( $excluded, get_current_user_id() );
    	}
    
    	$args['exclude'] = $excluded;
    
    	return $args;
    }
    
    add_filter( 'bp_after_core_get_users_parse_args', 'buddydev_exclude_logged_and_admin_users' );
    
    

    Regards
    Ravi

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #45624

    Hello Ravi, thank you, it’s working perfectly with Who’s Online.

    Any idea why it’s not working with the Dynamic Members List as well?

    Regards
    Carsten

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #45628

    Hello Carsten,

    Thank you for the acknowledgement. Please let me know what are Dynamic Members List and how is this list created so that I can check.

    Regards
    Ravi

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #45629

    Hi Ravi, the Dynamic Members List is part of the BP widget package.

    I think I have posted this topic before, my Apologies, is it something about that these widgets have a different behavior than eg. Who’s online?

    https://imgur.com/KVk4ghe

    Regards
    Carsten

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #45630

    Hello Carsten,

    The issue was BuddyPress using the rest API calls to list dynamic member lists which is why our members were not excluded in this case. Please try the following filter with the same code

    
    add_filter( 'bp_rest_members_get_items_query_args', 'buddydev_exclude_logged_and_admin_users' );
    
    

    Please check

    Regards
    Ravi

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #45631

    Hello Ravi, great job, it worked.

    Thanks very much for your help.

    Regards
    Carsten

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #45642

    Hello Carsten,

    Thank you for the acknowledgement. I am glad that I could help.

    Regards
    Ravi

The topic ‘ [Resolved] Exclude admin from all members lists?’ is closed to new replies.

This topic is: resolved