Fix code for wildcard user search using wp_user_query
-
I have a site with tens of thousands of users, however some of these are spam accounts. I need to get to a true number of subscribers so I am searching the site to identify and expunge the spammers. I have a useful list of keywords which is revealing many of them.
However when searching for users in /wp-admin/users.php I noticed that it only returns exact matches for email addresses. For example if I search for the term ‘handbag’ it will return a user with the name ‘handbag’ but not a user whose email address is ‘[email protected]’.
I’ve had a look through the codex and forums and found this likely-looking piece of code which hooks into wp_user_query to offer exactly the functionality I’m looking for:
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; } }
(It was posted by Gon?alo Peres in this thread).
However when I implement the code on my site I get the following errors:
Warning: mysql_real_escape_string(): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /var/www/(site URL)/functions.php on line 431
Warning: mysql_real_escape_string(): A link to the server could not be established in /var/www/(site URL)/functions.php on line 431My knowledge of PHP is only very basic and I’m having a hard time trying to figure out what’s wrong. If anyone can solve it for me or at least point me in the right direction I’d be very grateful. I’m sure there must be a solution to this as there must be other people in a similar situation.
- The topic ‘Fix code for wildcard user search using wp_user_query’ is closed to new replies.