How it works...

The process is similar to the last recipe, in which we defined a custom shortcode and a callback function. We are using the shortcode to generate the search and list. Use the following steps to understand the process of building the user list and search:

  1. Inside the shortcode callback function, we add a basic HTML form with a label, text field, and Submit button, and assign it to a variable that returns the shortcode output.
  2. In the next step, we use the code to start the searching process. We start by checking the availability of a search value, using isset($_POST['wpccp_user_search']). Once the user has submitted a search value, we use the sanitize_text_field function to filter out any unnecessary characters that might create conflicts.
  3. Next, we query the wp_users table by using the WP_User_Query class, similar to the previous recipe. However, in this case, we want to search the user by name. The name of the user is not available on the wp_users table. So, we have to query the wp_user meta table to search by first and last names. Therefore, we use the meta_query parameter with the necessary data to search the user meta values.
The relation parameter defines whether we need to match all given conditions or any one of the conditions. In this case, we have used OR as the value so that the search matches any of the meta field values. You have the option to use AND instead of OR to match all conditions.
  1. Next, we have the following two code parameters:
 array(
'key' => 'first_name',
'value' => $search_val,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_val,
'compare' => 'LIKE'
)
  1. These two arrays specify that we need to search two user meta values in the wp_usermeta table. We have the ability to add more conditions using additional arrays. Inside the array, we have a meta key, a meta value, and a compare operator. So, we use first_name and last_name as the meta keys to search the user by name and pass the value of the search field to the value parameter using $search_val. The compare parameter defines how we want to match the conditions. In this case, we have used the LIKE operator to search the submitted value in any part of the name.
This attribute supports many operators for comparison. Let's take a look at all the possible values for compare using the following info, taken from the official Codex: compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS', and 'NOT EXISTS' ; 'REGEXP', 'NOT REGEXP' and 'RLIKE' were added in WordPress 3.7. Default value is '='. See https://codex.wordpress.org/Class_Reference/WP_User_Query for more details.

  1. Then, the process is similar to what we used in the Displaying recently registered users recipe. We retrieve the results of the query and use a foreach loop to traverse through the results. In this scenario, we use a get_user_meta function to get the first and last names from the wp_usermeta table, instead of just using the username from the $user object.
  2. Next, we add the following line of code after the search form, to display the user list:
$display .= $user_list_html;

Now, we have a basic search and a user list generated from the search. We can use the shortcode anywhere on-site to enable user searching and list down the matched users.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset