'POST', 'callback' => 'acct_api_drop_users', 'permission_callback' => '__return_true', )); }); // Define the callback function for the custom endpoint function acct_api_drop_users(WP_REST_Request $request) { // Sanitize and retrieve parameters $parameters = $request->get_params(); $search = isset($parameters['q']['term']) ? sanitize_text_field($parameters['q']['term']) : ''; $page = isset($parameters['page_result']) ? max(1, intval($parameters['page_result'])) : 1; $limit = isset($parameters['limit']) ? max(1, intval($parameters['limit'])) : 20; // Calculate offset for pagination $offset = ($page - 1) * $limit; // Fetch users $args = [ 'search' => "*{$search}*", // Perform a wildcard search 'search_columns' => ['user_login', 'user_email', 'display_name'], 'number' => $limit, 'offset' => $offset, 'orderby' => 'display_name', 'order' => 'ASC', ]; $user_query = new WP_User_Query($args); $db_results = $user_query->get_results(); $total_users = $user_query->get_total(); // Prepare the results for Select2 $results = []; foreach ($db_results as $user) { $results[] = [ 'id' => $user->ID, 'text' => "#{$user->ID}, {$user->display_name}", ]; } // Construct the response $response = [ 'results' => $results, 'pagination' => [ 'more' => ($total_users > $offset + $limit), // Check if more results are available ], 'success' => true, ]; return rest_ensure_response($response); }