For those still wanting to search in the email field, like before, you can add these lines of code to your theme “functions.php” file:
if(is_admin()) {
add_action( 'pre_user_query', 'user_search_by_email' );
function user_search_by_email($wp_user_query) {
if(false === strpos($wp_user_query->query_where, '@')) {
$wp_user_query->query_where = substr(trim($wp_user_query->query_where), 0, -1 )." OR user_email LIKE '%".mysql_real_escape_string($_GET["s"])."%')";
}
return $wp_user_query;
}
}
It’s so small, that i don’t think i needed a plugin just for this.
I had to come up with this, because people were used to search users for parts of the the email (ex: “abc” to match “abcde@email.com”).
Hope this helps.
Cheers!
you can add these lines of code to your theme “functions.php” file:
It woudl be better for something like this to go in the mu-plugins folder. This is exactly the kind of code you would place there and you don’t need a plugin header nor does it need to be activated.
It is not as appropriate to place it in theme files, as it’s not theme dependent.
I understand that Andrea. Since i’m using a regular WordPress 3.x installation (not the MU), is there an “equivalent folder” where i can store this lines of code?
TIA.
Same folder. It’s not even mu anymore in the code, and as the codex page I linked to explains, this folder has been in WordPress single since 2.8.
mu-plugins is a backronym. Doesn’t mean it’s for multisite only.
For those still wanting to search in the email field, like before, you can add these lines of code to your theme “functions.php” file:
if(is_admin()) {
add_action( ‘pre_user_query’, ‘user_search_by_email’ );
function user_search_by_email($wp_user_query) {
if(false === strpos($wp_user_query->query_where, ‘@’)) {
$wp_user_query->query_where = substr(trim($wp_user_query->query_where), 0, -1 ).” OR user_email LIKE ‘%”.mysql_real_escape_string($_GET[“s”]).”%’)”;
}
return $wp_user_query;
}
}
Definitely a great alternative. Many of those who participate in this discussions play smart but only the rare ones give really a smart solution to solve a issue. Congratulations and many thanks!
Please correct the above code with this instead:
if(is_admin()) {
add_action( 'pre_user_query', 'user_search_by_email' );
function user_search_by_email($wp_user_query) {
if(false === strpos($wp_user_query->query_where, '@') && !empty($_GET["s"])) {
$wp_user_query->query_where = str_replace(
"user_nicename LIKE '%".mysql_real_escape_string($_GET["s"])."%'",
"user_nicename LIKE '%".mysql_real_escape_string($_GET["s"])."%' OR user_email LIKE '%".mysql_real_escape_string($_GET["s"])."%'",
$wp_user_query->query_where);
}
return $wp_user_query;
}
}
Sorry folks, but the previous code prevented from searching users by role. This approach is better and corrects that.
@gonçalo Thanks for the code.