{"id":40,"date":"2019-11-11T14:04:19","date_gmt":"2019-11-11T20:04:19","guid":{"rendered":"https:\/\/travisolson.net\/code\/?p=40"},"modified":"2019-11-11T14:06:05","modified_gmt":"2019-11-11T20:06:05","slug":"wordpress-quick-user-search-by-email-address","status":"publish","type":"post","link":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/","title":{"rendered":"WordPress: Quick User Search by Email Address"},"content":{"rendered":"<p>Sometimes it is nice to have a way to quickly search for users\/customers without having to use the WordPress default user search.\u00a0 Especially when your user base is large, this can be a very slow process.\u00a0 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.<\/p>\n<p>The following code is intended to be added to a plugin &#8212; presumably a custom plugin.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\nclass MyPrefix_User_By_Email {\r\n  var $settings;\r\n  \r\n  function __construct() {\r\n    \/\/ vars\r\n    $this-&gt;settings = array();\r\n    $this-&gt;plugin_path = dirname(__FILE__); \/\/ Change this if this file is not in the root of the plugin.\r\n    $this-&gt;js_uri = plugin_dir_url(__FILE__) . 'assets\/js\/';\r\n    $this-&gt;img_uri = plugin_dir_url(__FILE__) . 'assets\/images\/';\r\n    $this-&gt;page_name = 'myprefix_user_by_email';\r\n    $this-&gt;page_url = 'admin.php?page='.$this-&gt;page_name;\r\n    \r\n    add_action('init', array($this, 'init'), 99); \/\/ this needs to fire after post types have been declared for the printing to work correctly\r\n    add_action('wp_before_admin_bar_render', array($this, 'admin_bar_search_menu'), 800 );\r\n  }\r\n  \r\n  function init() {\r\n    \/\/ admin only\r\n    if( is_admin() ) {\r\n      add_action('admin_menu', array($this, 'admin_menu'), 30);\r\n    }\r\n  }\r\n  \r\n  function admin_menu() \t{\r\n    \/* Add custom pages. If first argument is null then it won't add a submenu item. Otherwise use 'edit.php?post_type=&lt;post_type&gt;'. *\/\r\n    add_submenu_page('users.php', 'Search By Email', 'Search By Email', 'list_users', $this-&gt;page_name, array($this, 'search_by_email'));\r\n  }\r\n  \r\n  function admin_bar_search_menu($wp_admin_bar) {\r\n    global $wp_admin_bar;\r\n    \r\n    if ( !myprefix_is_admin() )\r\n      return;\r\n\r\n    $form  = '&lt;form action=\"' . admin_url($this-&gt;page_url) . '\" id=\"adminbar-user-search-form\" method=\"post\"&gt;';\r\n    $form .= '&lt;input class=\"adminbar-input\" name=\"user_email\" id=\"adminbar-user-search\" type=\"text\" value=\"\" placeholder=\"User email address\" maxlength=\"150\" \/&gt;';\r\n    $form .= '&lt;input type=\"submit\" class=\"adminbar-button\" value=\"' . __('Search') . '\"\/&gt;';\r\n    $form .= '&lt;\/form&gt;';\r\n  \r\n    $wp_admin_bar-&gt;add_menu( array(\r\n      'parent' =&gt; false,\r\n      'id'     =&gt; 'user-email-search',\r\n      'title'  =&gt; $form,\r\n      'meta'   =&gt; array(\r\n        'class'    =&gt; 'admin-bar-user-search',\r\n        'tabindex' =&gt; -1,\r\n      )\r\n    ) );\r\n  }\r\n\r\n  function search_by_email() {\r\n    global $wpdb;\r\n    ?&gt;\r\n    &lt;h1&gt;Search By Email Address&lt;\/h1&gt;\r\n    \r\n    &lt;?php\r\n    $this-&gt;print_form();\r\n  }\r\n  \r\n  function print_form() {\r\n    global $wpdb;\r\n    \r\n    $found_users = false;\r\n    if (isset($_POST['user_email'])) {\r\n      $sql = $wpdb-&gt;prepare(\"SELECT U.ID, U.user_email,\r\n          UM1.meta_value as 'first_name',\r\n          UM2.meta_value as 'last_name'\r\n        FROM wp_users U\r\n        LEFT OUTER JOIN wp_usermeta UM1 ON UM1.user_id = U.ID AND UM1.meta_key = 'first_name'\r\n        LEFT OUTER JOIN wp_usermeta UM2 ON UM2.user_id = U.ID AND UM2.meta_key = 'last_name'\r\n        WHERE user_email = %s\", $_POST['user_email']);\r\n      $found_users = $wpdb-&gt;get_results($sql);\r\n      if (count($found_users) == 1) {\r\n        wp_redirect('\/wp-admin\/user-edit.php?user_id=' . $found_users[0]-&gt;ID);\r\n        exit();\r\n      }\r\n    }\r\n    \r\n    include($this-&gt;plugin_path . '\/templates\/user_by_email.php');\r\n  }\r\n}\r\n\r\nfunction myprefix_init_user_by_email() {\r\n  global $MyPrefix_User_By_Email;\r\n  if( !isset($MyPrefix_User_By_Email) ) {\r\n    $MyPrefix_User_By_Email = new MyPrefix_User_By_Email();\r\n  }\r\n\r\n  return $MyPrefix_User_By_Email;\r\n}\r\n\r\n\r\n\/\/ initialize\r\nmyprefix_init_user_by_email();\r\n?&gt;<\/pre>\n<p>Here is the template file, named <em>user_by_email.php<\/em>, that is references in the print_form() function above.\u00a0 In this example, it is located in the <em>templates<\/em> subdirectory.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;p&gt;Search for an EXACT email address.&lt;\/p&gt;\r\n&lt;form method=\"post\" action=\"\"&gt;\r\n&lt;table class=\"data-list\" style=\"width:auto;\"&gt;\r\n  &lt;tr&gt;\r\n    &lt;th&gt;Email Address&lt;\/th&gt;\r\n    &lt;td&gt;&lt;input type=\"text\" name=\"user_email\" value=\"&lt;?php echo isset($_POST['user_email']) ? esc_attr($_POST['user_email']) : ''; ?&gt;\" style=\"width:400px;\"&gt;&lt;\/td&gt;\r\n  &lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n&lt;input type=\"submit\" name=\"btnSave\" value=\"Search\"&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt;?php\r\nif ($found_users) {\r\n  echo '&lt;h3&gt;Results&lt;\/h3&gt;';\r\n  foreach ($found_users as $user) {\r\n  ?&gt;\r\n  &lt;div&gt;&lt;strong&gt;&lt;a href=\"\/wp-admin\/user-edit.php?user_id=&lt;?php echo $user-&gt;ID; ?&gt;\"&gt;&lt;?php echo $user-&gt;first_name . ' ' . $user-&gt;last_name; ?&gt;&lt;\/a&gt;&lt;\/strong&gt;&lt;\/div&gt;\r\n  &lt;div&gt;User ID: &lt;?php echo $user-&gt;ID; ?&gt;&lt;\/div&gt;\r\n  &lt;?php\r\n  }\r\n}\r\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it is nice to have a way to quickly search for users\/customers without having to use the WordPress default user search.\u00a0 Especially when your user base is large, this can be a very slow process.\u00a0 This code adds a quick search feature to the top admin bar, as well as submenu item that appears&#8230;  <a class=\"excerpt-read-more\" href=\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/\" title=\"Read WordPress: Quick User Search by Email Address\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>WordPress: Quick User Search by Email Address - Travis Olson&#039;s Code Snippets<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WordPress: Quick User Search by Email Address - Travis Olson&#039;s Code Snippets\" \/>\n<meta property=\"og:description\" content=\"Sometimes it is nice to have a way to quickly search for users\/customers without having to use the WordPress default user search.\u00a0 Especially when your user base is large, this can be a very slow process.\u00a0 This code adds a quick search feature to the top admin bar, as well as submenu item that appears... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/\" \/>\n<meta property=\"og:site_name\" content=\"Travis Olson&#039;s Code Snippets\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-11T20:04:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-11T20:06:05+00:00\" \/>\n<meta name=\"author\" content=\"Travis Olson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Travis Olson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/\"},\"author\":{\"name\":\"Travis Olson\",\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"headline\":\"WordPress: Quick User Search by Email Address\",\"datePublished\":\"2019-11-11T20:04:19+00:00\",\"dateModified\":\"2019-11-11T20:06:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/\"},\"wordCount\":117,\"publisher\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"articleSection\":[\"WordPress\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/\",\"url\":\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/\",\"name\":\"WordPress: Quick User Search by Email Address - Travis Olson&#039;s Code Snippets\",\"isPartOf\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#website\"},\"datePublished\":\"2019-11-11T20:04:19+00:00\",\"dateModified\":\"2019-11-11T20:06:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.travisolson.net\/code\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WordPress: Quick User Search by Email Address\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.travisolson.net\/code\/#website\",\"url\":\"https:\/\/www.travisolson.net\/code\/\",\"name\":\"Travis Olson&#039;s Code Snippets\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.travisolson.net\/code\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934\",\"name\":\"Travis Olson\",\"logo\":{\"@id\":\"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.travisolson.net\/code\/author\/travis-olson\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WordPress: Quick User Search by Email Address - Travis Olson&#039;s Code Snippets","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/","og_locale":"en_US","og_type":"article","og_title":"WordPress: Quick User Search by Email Address - Travis Olson&#039;s Code Snippets","og_description":"Sometimes it is nice to have a way to quickly search for users\/customers without having to use the WordPress default user search.\u00a0 Especially when your user base is large, this can be a very slow process.\u00a0 This code adds a quick search feature to the top admin bar, as well as submenu item that appears... Read more &raquo;","og_url":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/","og_site_name":"Travis Olson&#039;s Code Snippets","article_published_time":"2019-11-11T20:04:19+00:00","article_modified_time":"2019-11-11T20:06:05+00:00","author":"Travis Olson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Travis Olson","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/#article","isPartOf":{"@id":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/"},"author":{"name":"Travis Olson","@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"headline":"WordPress: Quick User Search by Email Address","datePublished":"2019-11-11T20:04:19+00:00","dateModified":"2019-11-11T20:06:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/"},"wordCount":117,"publisher":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"articleSection":["WordPress"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/","url":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/","name":"WordPress: Quick User Search by Email Address - Travis Olson&#039;s Code Snippets","isPartOf":{"@id":"https:\/\/www.travisolson.net\/code\/#website"},"datePublished":"2019-11-11T20:04:19+00:00","dateModified":"2019-11-11T20:06:05+00:00","breadcrumb":{"@id":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.travisolson.net\/code\/wordpress-quick-user-search-by-email-address\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.travisolson.net\/code\/"},{"@type":"ListItem","position":2,"name":"WordPress: Quick User Search by Email Address"}]},{"@type":"WebSite","@id":"https:\/\/www.travisolson.net\/code\/#website","url":"https:\/\/www.travisolson.net\/code\/","name":"Travis Olson&#039;s Code Snippets","description":"","publisher":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.travisolson.net\/code\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/0170ab5b2e7a3d88436375cc50336934","name":"Travis Olson","logo":{"@id":"https:\/\/www.travisolson.net\/code\/#\/schema\/person\/image\/"},"url":"https:\/\/www.travisolson.net\/code\/author\/travis-olson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":4,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":44,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/posts\/40\/revisions\/44"}],"wp:attachment":[{"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travisolson.net\/code\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}