Support

Account

Home Forums Front-end Issues User Results: Change Text Display on Autocomplete Reply To: User Results: Change Text Display on Autocomplete

  • Hi John, thank you for your response!

    I reached out to WP Engine, and between both responses and more researching/ testing I was able to start resolving this issue. They did inform me it is possible to define for variations.

    I’m just wondering now if there is a way to “hide” the user roles when autocomplete results pop up in select2 container. I tried with CSS (targeting CSS class for .select2-results__group) but that was no good. I’m not sure if this is something I can change in php.

    As an example, I’m including the code that worked for me – in case it might help someone else, since this is all undocumented and took a lot of trial/ error to find a solution. Thank you for leading me in the right direction!

    This worked for me for changing text display on results for user, to show custom image field, display name and email from user:

    add_filter('acf/fields/user/result/key=field_6329a6dc6410d', 'my_user_display_results', 10, 4); 
    			  
    function my_user_display_results ( $text, $user, $field, $post_id ) {
    	
    	$image = get_user_meta( $user->ID, 'my_local_avatar', true );
    	
    	if($image) {
    	$image_src = wp_get_attachment_image_src( $image, 'large' );
    	
    	$new_text .= '<img style="width: 30px; height: 30px; object-fit: cover; border-radius: 100px; float: left; position: relative; vertical-align: middle;" src="' . esc_url($image_src[0]) . '" alt="' . '" decoding="async" loading="lazy" width="150" height="150"><h3 style="font-size: 15px; display: inline; vertical-align: middle; margin-left: 10px;">' . esc_html($user->display_name) .  '</h3><h3 style="font-size: 14px; display: inline; vertical-align: middle; margin-left: 10px;">(' .  esc_html($user->user_email) . ')</h3>';
    	}
    	else {
    	$new_text .= '<h3 style="font-size: 15px; display: inline; vertical-align: middle; margin-left: 10px;">' . esc_html($user->display_name) .  '</h3><h3 style="font-size: 14px; display: inline; vertical-align: middle; margin-left: 10px;">(' .  esc_html($user->user_email) . ')</h3>';
    	}
        return $new_text;
    }

    This worked for me for query of user, so I could narrow user search by display name or email:

    add_filter('acf/fields/user/query/key=field_6329a6dc6410d', 'filter_my_user_by_fields', 10, 3);
    
    function filter_my_user_by_fields( $args, $field, $user_id ) {
        $the_search = $args['s'];
    	unset($args['s']);
    	$args['search_columns'] = array(
    		'relation' => 'OR',
    	    array(
    		    'key' => 'display_name',
    		    'value' => $the_search,
    		    'compare' => 'LIKE',
    		),
    		array(
    		     'key' => 'email',
    		     'value' => $the_search,
    		     'compare' => 'LIKE',
    		)
    	); 
        return $args;
    }

    Is the user role able to be hidden on frontend select2? I’m not sure if this is something I can alter in the code I already have for results, or if this is something unalterable. It would be nice to not let users know who’s got what level of access/ capabilities.

    In ACF, I have user field set to filter for select user roles (I didn’t set the filter in any $args for results as you can see above). I’m not sure if I should have set args since the ACF field already had the filter options I needed.

    Please advise if hiding user roles is possible on frontend.