BuddyDev

Search

[Resolved] Hide current logged in user and admin

  • Participant
    Level: Master
    Posts: 213
    smart life on #14441

    Hi,

    I found this:
    https://buddydev.com/buddypress-tricks/hide-logged-users-listings-directory-search-members-friends-list-widgets-etc-buddypress-site/

    Also this
    https://buddypress.org/support/topic/hide-admin-from-members-and-activity/

    Can you help me complete code for 2 request bellow:
    1) Hide CURRENT logged in user from search, widget, members directory.
    –> WHY? It so weried to see myself in members widget list.

    2) I would like to hide admin from search, members widget, directory.

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #14447

    Hi Julia,
    You can use the following code to achieve same.

    
    
    /**
     * Exclude logged and admin users from BuddyPress users list.
     *
     * @param array $args member loop args.
     *
     * @return array
     */
    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' );
    
    

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 213
    smart life on #14464

    Worked. Thanks

  • Participant
    Level: Master
    Posts: 213
    smart life on #14471

    Hi,

    Message buddypress
    ==> Send To (Username or Friend’s Name)

    I still see myself & admin.

    I would like to keep myself (current loggin). BUT REMOVE admin in message. Please help.

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #14473

    What you are seeing is the auto suggestion for message and not the members list.

    For the message autocomplete, the following code should work

    
    
    /**
     * Filter and remove admin and logged in users from the message autocomplete box.
     *
     * @param array $user_ids user ids.
     *
     * @return array
     */
    function buddydev_filter_message_auto_complete_ids( $user_ids ) {
    	if ( empty( $user_ids ) ) {
    		return $user_ids;
    	}
    
    	$excluded = get_users( array( 'role' => 'administrator', 'fields' => 'ID' ) );
    
    	if ( is_user_logged_in() ) {
    		array_push( $excluded, get_current_user_id() );
    	}
    
    	return array_diff( $user_ids, $excluded );
    }
    add_filter( 'bp_core_autocomplete_ids', 'buddydev_filter_message_auto_complete_ids' );
    add_filter( 'bp_friends_autocomplete_ids', 'buddydev_filter_message_auto_complete_ids' );
    
    

    Best Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 213
    smart life on #14484

    Hi,

    Can you please re-check. There is no work. I clear cache, re-login, etc.. still same

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #14503

    You are right, that code was for older version of BuddyPress.

    Please remove that and use the following

    
    
    /**
     * Filter and remove admin and logged in users from all BuddyPress auto complete box.
     *
     * @param array $args BP_User_Query args.
     *
     * @return array
     */
    function buddydev_filter_buddypress_auto_complete_ids( $args ) {
    	$user_ids = isset( $args['exclude'] ) ? $args['exclude'] : array();
    
    	if ( $user_ids && ! is_array( $user_ids ) ) {
    		$user_ids = wp_parse_id_list( $user_ids );
    	}
    
    	$excluded = get_users( array( 'role' => 'administrator', 'fields' => 'ID' ) );
    
    	if ( is_user_logged_in() ) {
    		array_push( $excluded, get_current_user_id() );
    	}
    
    	$args['exclude'] = array_merge( $excluded, $user_ids );
    	return $args;
    }
    add_filter( 'bp_members_suggestions_query_args', 'buddydev_filter_buddypress_auto_complete_ids' );
    
    

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 213
    smart life on #14511

    Thanks worked.

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #14530

    You are welcome.

The topic ‘ [Resolved] Hide current logged in user and admin’ is closed to new replies.

This topic is: resolved