wp_ajax_autocomplete_user

Ajax handler for user autocomplete.

Description

wp_ajax_autocomplete_user(); 

Usage

  1. if ( !function_exists( 'wp_ajax_autocomplete_user' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_autocomplete_user(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_autocomplete_user() { 
  2. if ( ! is_multisite() || ! current_user_can( 'promote_users' ) || wp_is_large_network( 'users' ) ) 
  3. wp_die( -1 ); 
  4.  
  5. /** This filter is documented in wp-admin/user-new.php */ 
  6. wp_die( -1 ); 
  7.  
  8. $return = array(); 
  9.  
  10. // Check the type of request 
  11. // Current allowed values are `add` and `search` 
  12. if ( isset( $_REQUEST['autocomplete_type'] ) && 'search' === $_REQUEST['autocomplete_type'] ) { 
  13. $type = $_REQUEST['autocomplete_type']; 
  14. } else { 
  15. $type = 'add'; 
  16.  
  17. // Check the desired field for value 
  18. // Current allowed values are `user_email` and `user_login` 
  19. if ( isset( $_REQUEST['autocomplete_field'] ) && 'user_email' === $_REQUEST['autocomplete_field'] ) { 
  20. $field = $_REQUEST['autocomplete_field']; 
  21. } else { 
  22. $field = 'user_login'; 
  23.  
  24. // Exclude current users of this blog 
  25. if ( isset( $_REQUEST['site_id'] ) ) { 
  26. $id = absint( $_REQUEST['site_id'] ); 
  27. } else { 
  28.  
  29. $include_blog_users = ( $type == 'search' ? get_users( array( 'blog_id' => $id, 'fields' => 'ID' ) ) : array() ); 
  30. $exclude_blog_users = ( $type == 'add' ? get_users( array( 'blog_id' => $id, 'fields' => 'ID' ) ) : array() ); 
  31.  
  32. $users = get_users( array( 
  33. 'blog_id' => false,  
  34. 'search' => '*' . $_REQUEST['term'] . '*',  
  35. 'include' => $include_blog_users,  
  36. 'exclude' => $exclude_blog_users,  
  37. 'search_columns' => array( 'user_login', 'user_nicename', 'user_email' ),  
  38. ) ); 
  39.  
  40. foreach ( $users as $user ) { 
  41. $return[] = array( 
  42. /** translators: 1: user_login, 2: user_email */ 
  43. 'label' => sprintf( _x( '%1$s (%2$s)', 'user autocomplete result' ), $user->user_login, $user->user_email ),  
  44. 'value' => $user->$field,  
  45. ); 
  46.  
  47. wp_die( wp_json_encode( $return ) );