BuddyDev

Search

[Resolved] Hiding users from widgets not working

  • Participant
    Level: Initiated
    Posts: 3
    berzerk on #51400

    Hello @sbrajesh,

    Awesome community you have here! I am trying to get this code to work from your post – https://buddydev.com/hiding-users-on-buddypress-based-site/ to exclude admins inside the BuddyPress block widgets but it is not working. Maybe it is not working with the new major update from BuddyPress. It is working for the members directory only. How can we get this to work everywhere like the code suggests? (directories/widgets/friend lists etc)

    BuddyPress version: 12.1.1

    Cheers for your assistance.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #51410

    Hi,
    Welcome to BuddyDev support.

    You are right about the issue.

    The problem is BuddyPress does not provide any hook to filter for user list.
    https://github.com/buddypress/buddypress/blob/master/src/bp-members/bp-members-blocks.php#L26

    There is a solution using bp_after_core_get_users_parse_args
    I haven’t posted it as it will run for all user queries irrespective of their context. That makes it inefficient for use.

    If you still want to proceed with it, Please let me know and I will share the code.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 3
    berzerk on #51411

    It is good to be here!

    Thank you for the caution and explanation, much appreciated. And thank you for your willingness to share the code, I’d like to try your code.

    Cheers.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #51419

    Hi,
    Thank you for your patience.

    Please try using the following code

    
    
    /**
     * Exclude Users from BuddyPress Members List by WordPress role.
     *
     * @param array $args args.
     *
     * @return array
     */
    function buddydev2_exclude_users_by_role( $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 );
    
    	$args['exclude'] = $excluded;
    
    	return $args;
    }
    
    add_filter( 'bp_after_core_get_users_parse_args', 'buddydev2_exclude_users_by_role' );
    

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 3
    berzerk on #51427

    Hello Brajesh,

    You are the GOAT! I would like to exclude contributor accounts too. Is the below code how you would do it? Sourced from one of your comments on the original blog posts.

    /**
     * Exclude Users from BuddyPress Members List by WordPress role.
     *
     * @param array $args args.
     *
     * @return array
     */
    function buddydev2_exclude_users_by_role( $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 );
    	}
    
    	$user_ids =  get_users( array( 'role__in' => ['administrator' , 'contributor'] ,'fields'=>'ID') );
    
    	$excluded = array_merge( $excluded, $user_ids );
    
    	$args['exclude'] = $excluded;
    
    	return $args;
    }
    
    add_filter( 'bp_after_core_get_users_parse_args', 'buddydev2_exclude_users_by_role' );
  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #51453

    Thank you.
    Yes, it would work.

    Please let me know if it worked for you or not?

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved