edd_ajax_search_users

Searches for users via ajax and returns a list of results.

Description

edd_ajax_search_users(); 

Usage

  1. if ( !function_exists( 'edd_ajax_search_users' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'easy-digital-downloads/includes/ajax-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = edd_ajax_search_users(); 
  7.  

Defined (1)

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

/includes/ajax-functions.php  
  1. function edd_ajax_search_users() { 
  2.  
  3. if( current_user_can( 'manage_shop_settings' ) ) { 
  4.  
  5. $search_query = trim( $_POST['user_name'] ); 
  6. $exclude = trim( $_POST['exclude'] ); 
  7.  
  8. $get_users_args = array( 
  9. 'number' => 9999,  
  10. 'search' => $search_query . '*' 
  11. ); 
  12.  
  13. if ( ! empty( $exclude ) ) { 
  14. $exclude_array = explode( ', ', $exclude ); 
  15. $get_users_args['exclude'] = $exclude_array; 
  16.  
  17. $get_users_args = apply_filters( 'edd_search_users_args', $get_users_args ); 
  18.  
  19. $found_users = apply_filters( 'edd_ajax_found_users', get_users( $get_users_args ), $search_query ); 
  20.  
  21. $user_list = '<ul>'; 
  22. if( $found_users ) { 
  23. foreach( $found_users as $user ) { 
  24. $user_list .= '<li><a href="#" data-userid="' . esc_attr( $user->ID ) . '" data-login="' . esc_attr( $user->user_login ) . '">' . esc_html( $user->user_login ) . '</a></li>'; 
  25. } else { 
  26. $user_list .= '<li>' . __( 'No users found', 'easy-digital-downloads' ) . '</li>'; 
  27. $user_list .= '</ul>'; 
  28.  
  29. echo json_encode( array( 'results' => $user_list ) ); 
  30.  
  31. die();