
Blackbams Blog
development – digital arts – internet
Knowledge is free. No one may take possession of it.
Recently, when working on a WordPress website for a huge amount of users, a customer was unsatisfyied with the WordPress backend search for users. The default backend search only is searching for your search string in usernames, but not for first name, last name or email of users.
As most WordPress projects are barely designed to have a huge amoung of users, this is not necessary in most cases. Anyway, the following code will help with this problem - add this to your functions.php-file and the WordPress user search will scan first name, last name and e-mail of every user.
Download the official WordPress Plugin
// the actual improvement of the query
function user_search_by_multiple_parameters($wp_user_query) {
if(false === strpos($wp_user_query -> query_where, '@') && !empty($_GET["s"])) {
global $wpdb;
$uids=array();
// get the custom meta fields to search
$iusib_custom_meta = get_option('iusib_meta_fields');
$iusib_cma = array_map('trim', explode(",",$iusib_custom_meta));
$iusib_add = "";
// the escaped query string
$qstr = mysql_real_escape_string($_GET["s"]);
// add all custom fields into the query
if(!empty($iusib_cma)) {
$iusib_add = " OR meta_key='".implode("' OR meta_key='",$iusib_cma)."'";
}
$usermeta_affected_ids = $wpdb -> get_results("SELECT DISTINCT user_id FROM ".$wpdb->base_prefix."usermeta WHERE (meta_key='first_name' OR meta_key='last_name'".$iusib_add.") AND LOWER(meta_value) LIKE '%".$qstr."%'");
foreach($usermeta_affected_ids as $maf) {
array_push($uids,$maf->user_id);
}
$users_affected_ids = $wpdb -> get_results("SELECT DISTINCT ID FROM ".$wpdb->base_prefix."users WHERE LOWER(user_nicename) LIKE '%".$qstr."%' OR LOWER(user_email) LIKE '%".$qstr."%'");
foreach($users_affected_ids as $maf) {
if(!in_array($maf->ID,$uids)) {
array_push($uids,$maf->ID);
}
}
$id_string = implode(",",$uids);
$wp_user_query -> query_where = str_replace("user_nicename LIKE '%".$qstr."%'", "ID IN(".$id_string.")", $wp_user_query -> query_where);
}
return $wp_user_query;
}
Dieser Eintrag wurde am 27. June 2011 um 23:23 in der Kategorie Plugins, WordPress, WP Scripts veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.
Warning: Use of undefined constant Ext_related_links - assumed 'Ext_related_links' (this will throw an Error in a future version of PHP) in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 75
Already 74 comments belonging to "WordPress improved user search (first name, last name, email) in backend":
Kommentare abonnieren (RSS) or URL Trackback
Donty
says:
on 19. February 2013 at 13:44 on clock
Hi Blackbam
Thanks for working on this, it does fit a surprising hole in sensible admin. Is the plugin updated on WP repository now? I have a 3..5.1 multisite that would benefit from this search enhancement but it is not yet working. Simply don't see any settings options or improvement on search eg search for email address.
Am I missing anything?
Thanks again
D
Blackbam
says:
on 19. February 2013 at 14:37 on clock
Hello Donty
this is the second request for full multisite support within two days I think it is time to implement it.
The Plugin currently IS working with multisite in the various subsites, but not in the screens showing all of the users (network administrator). I suppose that this is a different WordPress function. The Plugin will be updated in a few days/weeks with the aim of full multisite support.
Lee
says:
on 28. February 2013 at 23:11 on clock
I installed your plugin and I can't seem to get it to return a user by only a first or last name search.
I am running WP 3.5.1
Event Espresso 3.1.30.1
S2Member 130221
BuddyPress 1.6.4
Are any of these plugins incompatible with your plugin? Thanks.
Blackbam
says:
on 01. March 2013 at 11:17 on clock
Hello Lee, currently I do not KNOW about incompatibilities with any of this Plugins. But I can imagine very well that one of these Plugins, especially S2Member or BuddyPress are hooking into WordPress in a way, so the improved user search is not working.
Please can you deactivate each of this Plugins (preferrably in your test environment or maintenance mode) just to check if the improved user search is working and tell me which Plugin was responsible for the Problem? I can hopefully make an update to be compatible with this Plugin after some time then.
Lee
says:
on 01. March 2013 at 18:01 on clock
Black,
Thank you for the timely response! It is s2member that is blocking the use of your great plugin! We need s2member for the permissions it allows us to set individually. It's also a pretty popular plugin.
Basically s2member re formats the All Users dashboard to handle more data. I'm sure that's why there is a conflict.
Is this something you'll be looking into or putting on the back burner until there are more requests for it? Thank you.
Blackbam
says:
on 01. March 2013 at 20:37 on clock
Hey again I am going to have a look at S2Member compatibility at some time in the future, but currently my timetable is very full.
I think that s2member is reorganizing a lot of WPs' internal user structure, so this is why the Plugin is not working.
But you could take the code of the Plugin and try to integrate this with S2Member as the code of "Improved User Search" is quite simple. You just have to find the correct hook inside of S2Member.
marios
says:
on 29. March 2013 at 00:48 on clock
I have a mutlisite blog. I also use the cimy user extra fields.. I want to search users according to some filelds from this plugin. Is it possible?
Blackbam
says:
on 29. March 2013 at 15:27 on clock
Hello, yes I think this should be possible (as they are using the WordPress Standard to save additional user information). Just add the name/id of the user meta fields into the box of the "User Search" administration page.
kyle burnett
says:
on 23. October 2013 at 02:51 on clock
question... why join in all the meta keys via this method...
if(!empty($iusib_cma)) {
$iusib_add = " OR meta_key='".implode("' OR meta_key='",$wpdb->escape($iusib_cma))."'";
}
$usermeta_affected_ids = $wpdb->get_results("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE (meta_key='first_name' OR meta_key='last_name'".$iusib_add.") AND LOWER(meta_value) LIKE '%".$qstr."%'");
vs. just directly querying the meta table like so...
$usermeta_affected_ids = $wpdb->get_results("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE LOWER(meta_value) LIKE '%".$qstr."%'");
just curious about the theory there.
great plugin. super helpful.
Blackbam
says:
on 05. November 2013 at 21:17 on clock
@kyle: Because we want to have control over the meta keys to be searched in - otherwise the query might find users which we do not want to find because of the given search input.
Max
says:
on 07. November 2013 at 16:10 on clock
Hi Blackbam,
Any plan to update it for multisite?
I think that it could become a must have for any multisite admin since there's always a lot of user in there.
Thanks
Blackbam
says:
on 08. January 2014 at 20:51 on clock
Hello, sorry for the late answer. Yes, there is a plan for doing that, but currently time is missing.
Arial Burnz
says:
on 14. July 2014 at 20:27 on clock
I don't see anywhere on your website where I can make a donation. I used this plugin and am VERY grateful for it. I'd like to show you my support by making a donation for your efforts. Thank you!!
Blackbam
says:
on 15. July 2014 at 15:18 on clock
Hello Arial, thank you. You are right it I did not include any donation possibility for this Plugin maybe I will add this in some time. For some of my Plugins there is a donation possibility so if you donate for one of my other Plugins here I know that this donation is meant to be for the custom user search :-)
Ellis Benus Web Designer in Columbia MO
says:
on 30. July 2014 at 17:44 on clock
I just tried to download the plugin from the WordPress Repository but there is no PHP plugin file...? I downloaded the plugin to my machine and there is a readme file and language packs...? I'm very interested in using your plugin, could you help with this conundrum? :(
Blackbam
says:
on 31. July 2014 at 17:27 on clock
Hello Ellis thanks a lot for the tip - there was a SVN problem with the most recent update - fixed now!
Jody B Roach
says:
on 31. July 2014 at 19:24 on clock
Hey, great plugin. I am though experiencing an issue. When i try to hit the wp-admin on a clean cache, I am getting the infamous white screen. THOUGH, I do have a cached login from last night running, and I am able to browse around freely, until i try to hit a page i dont visit so often..like removing a plugin, or removing some comments, then i get the white screen. Ideas?
Jody B Roach
says:
on 31. July 2014 at 19:33 on clock
Nevermind on last comment sir, fresh install of your updated plug from last night has all working perfect. Thanks!
Andreu
says:
on 22. September 2014 at 22:10 on clock
Hi! Your plugin is working me only for the first admin user (ID = 1). Not for the rest of users, even they are admins. Do you know which can be the problem there? Thanks
Blackbam
says:
on 03. October 2014 at 16:57 on clock
Hello I cannot say if it is a problem with your system but for WordPress 4.0 I am going to release an update soon.
Phil
says:
on 27. November 2014 at 10:15 on clock
Hi Blackbam,
I used your plugin but had to change the regexp on line 97 to:
As you can see I added the underscore, because I wanted it to search in billing_company too, but your version skipped fields with underscores.
Thanks!
Blackbam
says:
on 11. April 2015 at 21:38 on clock
Hey thanks for pointing on this. I will integrate this in the next update.
Kathryn Reeve
says:
on 11. June 2015 at 12:11 on clock
Hi Blackbam,
I've got a patch I'd like to submit to you as the plugin no longer works on newer versions of PHP as well as $wpob->escape being deprecated
How can i go about getting them to you?
Thanks
Blackbam
says:
on 14. June 2015 at 16:44 on clock
Hi there
contacted you via e-mail. Looking forward to your answer.
Thanks
Leave a comment: