BuddyDev

Search

Hiding users on buddypress based site

  • Participant
    Level: Enlightened
    Posts: 57
    Katrine Spencer on #53476

    Hello

    This is a follow up question to the previous post:
    https://buddydev.com/hiding-users-on-buddypress-based-site/

    I use BuddyPress Global Search and this seems to be causing some issues with the code down below.

    I use the Global Search to fetch members and in this case when searching for any usernames that are hidden within the role from the code – it returns a critical error. If you go to the hidden members direct profile link they’re shown (which is fine) – I would like for this to work with Global Search too.

    Members should be able to search for usernames, but I just don’t want them to be shown on the directory page.

    It’s a problem when I have 2 users with the same or similar usernames.

    Example:
    JohnDoe (Basic – user role is excluded with code below)
    John (Standard – user shown and should be listed in Global Search).

    In this case none of the above is shown since it returns critical error when searching for “john”.

    Is this possible to fix somehow?

    /**
     * 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' );
  • Participant
    Level: Enlightened
    Posts: 57
    Katrine Spencer on #53477

    In this case I’m working with 3 user roles

    *Basic* This is the one set to hidden with the provided snippet
    User role: pmpro_role_1

    *Standard* This is visible
    User role: pmpro_role_2

    *Premium* This is visible
    User role: pmpro_role_2

    Also using the following setup:
    WordPress: Version 6.6.2
    BuddyPress: Version 14.2.1
    BuddyPress Global Search: Version 1.2.1

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #53480

    Hi Katrine,
    Thank you for reporting the issue.

    I am sorry, I don’t see any issue that it will cause with 3rd party code unless they are doing something strange. BuddyPress global search is abandoned(also unlisted from WPorg due to security issue)
    https://wordpress.org/plugins/buddypress-global-search/

    I am not sure which direction I should point you to. I will suggest if feasible, try using a different plugin.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved