Sometimes it is nice to have a way to quickly search for users/customers without having to use the WordPress default user search. Especially when your user base is large, this can be a very slow process. This code adds a quick search feature to the top admin bar, as well as submenu item that appears in the Users menu in the left navigation.
The following code is intended to be added to a plugin — presumably a custom plugin.
<?php
class MyPrefix_User_By_Email {
var $settings;
function __construct() {
// vars
$this->settings = array();
$this->plugin_path = dirname(__FILE__); // Change this if this file is not in the root of the plugin.
$this->js_uri = plugin_dir_url(__FILE__) . 'assets/js/';
$this->img_uri = plugin_dir_url(__FILE__) . 'assets/images/';
$this->page_name = 'myprefix_user_by_email';
$this->page_url = 'admin.php?page='.$this->page_name;
add_action('init', array($this, 'init'), 99); // this needs to fire after post types have been declared for the printing to work correctly
add_action('wp_before_admin_bar_render', array($this, 'admin_bar_search_menu'), 800 );
}
function init() {
// admin only
if( is_admin() ) {
add_action('admin_menu', array($this, 'admin_menu'), 30);
}
}
function admin_menu() {
/* Add custom pages. If first argument is null then it won't add a submenu item. Otherwise use 'edit.php?post_type=<post_type>'. */
add_submenu_page('users.php', 'Search By Email', 'Search By Email', 'list_users', $this->page_name, array($this, 'search_by_email'));
}
function admin_bar_search_menu($wp_admin_bar) {
global $wp_admin_bar;
if ( !myprefix_is_admin() )
return;
$form = '<form action="' . admin_url($this->page_url) . '" id="adminbar-user-search-form" method="post">';
$form .= '<input class="adminbar-input" name="user_email" id="adminbar-user-search" type="text" value="" placeholder="User email address" maxlength="150" />';
$form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
$form .= '</form>';
$wp_admin_bar->add_menu( array(
'parent' => false,
'id' => 'user-email-search',
'title' => $form,
'meta' => array(
'class' => 'admin-bar-user-search',
'tabindex' => -1,
)
) );
}
function search_by_email() {
global $wpdb;
?>
<h1>Search By Email Address</h1>
<?php
$this->print_form();
}
function print_form() {
global $wpdb;
$found_users = false;
if (isset($_POST['user_email'])) {
$sql = $wpdb->prepare("SELECT U.ID, U.user_email,
UM1.meta_value as 'first_name',
UM2.meta_value as 'last_name'
FROM wp_users U
LEFT OUTER JOIN wp_usermeta UM1 ON UM1.user_id = U.ID AND UM1.meta_key = 'first_name'
LEFT OUTER JOIN wp_usermeta UM2 ON UM2.user_id = U.ID AND UM2.meta_key = 'last_name'
WHERE user_email = %s", $_POST['user_email']);
$found_users = $wpdb->get_results($sql);
if (count($found_users) == 1) {
wp_redirect('/wp-admin/user-edit.php?user_id=' . $found_users[0]->ID);
exit();
}
}
include($this->plugin_path . '/templates/user_by_email.php');
}
}
function myprefix_init_user_by_email() {
global $MyPrefix_User_By_Email;
if( !isset($MyPrefix_User_By_Email) ) {
$MyPrefix_User_By_Email = new MyPrefix_User_By_Email();
}
return $MyPrefix_User_By_Email;
}
// initialize
myprefix_init_user_by_email();
?>
Here is the template file, named user_by_email.php, that is references in the print_form() function above. In this example, it is located in the templates subdirectory.
<p>Search for an EXACT email address.</p>
<form method="post" action="">
<table class="data-list" style="width:auto;">
<tr>
<th>Email Address</th>
<td><input type="text" name="user_email" value="<?php echo isset($_POST['user_email']) ? esc_attr($_POST['user_email']) : ''; ?>" style="width:400px;"></td>
</tr>
</table>
<input type="submit" name="btnSave" value="Search">
</form>
<?php
if ($found_users) {
echo '<h3>Results</h3>';
foreach ($found_users as $user) {
?>
<div><strong><a href="/wp-admin/user-edit.php?user_id=<?php echo $user->ID; ?>"><?php echo $user->first_name . ' ' . $user->last_name; ?></a></strong></div>
<div>User ID: <?php echo $user->ID; ?></div>
<?php
}
}
?>