Help with Modifying PHP Snippet

Could anyone help me modify the WordPress PHP snippet below so that the $role that is excluded applies only if the user has solely the specified role?

In other words, right now, the code will exclude a user with the role “administrator” even if they have other roles; I want the administrator role to be excluded only if they have no other roles.

/**
 * Exclude Users from BuddyPress Members List by WordPress role.
 *
 * @param array $args args.
 *
 * @return array
 */
function buddydev_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_has_members_parse_args', 'buddydev_exclude_users_by_role' );

– snippet from: buddydev.com/hiding-users-on-buddypress-based-site



Source link