Support

Account

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

Solved

User Results: Change Text Display on Autocomplete

  • Is it possible to modify text display of autocomplete results for User field type – similar to how one can modify text display of autocomplete results for Post Object or Relationship field types?

    I was successful using this for post object to modify text display of results:

    apply_filters( 'acf/fields/post_object/result', $text, $post, $field, $post_id );

    And to modify the search text display in query using this:

    apply_filters( 'acf/fields/post_object/query', $args, $field, $post_id );

    But I would like to do something similar now for User custom fields (User field type).

    For example, I would like to be able to add custom fields to User – and instead of the autocomplete showing each username, display name, and user role (which to me is a greater security risk) to modify the text to display user avatar, display name or custom field profile name, and user email. In context, I am using this to allow for frontend form edits by users.

    Is there a similar function I can use to hook into modifying the User results text display on autocomplete?

  • Yes, the user field has similar filter hooks available, undocumented

    acf/fields/user/query
    and
    acf/fields/user/result

    but it does not appear that there are variations available

    
    $item['text'] = apply_filters( 'acf/fields/user/result', $item['text'], $user, $query->field, $query->post_id );
    
    
    return apply_filters( 'acf/fields/user/query', $args, $query->field, $query->post_id );
    

    I do not know what is in the passed arguments

  • 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.

  • Never mind John, I got that user role issue fixed with CSS. Thank you again for your help!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.