I’ve been adding a couple of new features to Post Ideas+ over the past few days. One feature in particular required knowing the ID’s of users with the role of ‘administrator’. Now usually you can assume that the ID will be 1, as that’s what the initial adimin account setup by WordPress on install gets assigned. But as the code is going to be used in a plug-in you can’t really assume that.
Some people may have deleted the default account for security reasons (very good idea) or they have more that one administrator account. After hunting about on the WordPress forums for a while I managed to piece together a little snippet of code to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | //Get all admin user ID's in the DB function admin_user_ids(){ //Grab wp DB global $wpdb; //Get all users in the DB $wp_user_search = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users ORDER BY ID"); //Blank array $adminArray = array(); //Loop through all users foreach ( $wp_user_search as $userid ) { //Current user ID we are looping through $curID = $userid->ID; //Grab the user info of current ID $curuser = get_userdata($curID); //Current user level $user_level = $curuser->user_level; //Only look for admins if($user_level >= 8){//levels 8, 9 and 10 are admin //Push user ID into array $adminArray[] = $curID; } } return $adminArray; } //Usage $adminIdArray = $this->admin_user_ids(); |
I placed it into it’s own function within my plug-in Class so it can be called whenever needed. It could also be used for finding users with different levels in WordPress if needed. If you wanted you could modify the function to accept an argument admin_user_ids($the_user_level_i_need); allowing you to get the IDs of users at whatever level you like.
There seemed to be a few ways of doing this on the forums, but this one works for me at the moment.
Josh Salverda on July 5 10 / 185 Permalink
Hey, came across your blog post while searching for something else wordpress-related and just thought I would throw in my two cents…
Your function is quite nice but it seems really complicated and could get really slow over time as more users are added. The same thing can be handled just through one SQL query:
The above query can be modified to select more or less user data but thats the basic query which should get all the administrators in one go.
:)
Matt on July 5 10 / 185 Permalink
Perfect! Thanks Josh i’ll update the plug-in code to use that in the next version.
Brent Shepherd on November 1 11 / 304 Permalink
User level is deprecated as of WP 3.0 (http://codex.wordpress.org/Roles_and_Capabilities#Change_Log), so better to check if user has the administrator capability.
2
3
4
5
6
7
global $wpdb;
$ids = $wpdb->get_result( "SELECT wp_users.ID FROM wp_users WHERE (SELECT wp_usermeta.meta_value FROM wp_usermeta WHERE wp_usermeta.user_id = wp_users.ID AND wp_usermeta.meta_key = 'wp_capabilities') LIKE '%administrator%'" );
return $ids;
}
Brent Shepherd on November 1 11 / 304 Permalink
Just realised the table names were hardcoded in @josh’s original sql, much better version for distributed plugins is:
2
3
4
5
6
7
global $wpdb;
$ids = $wpdb->get_results( "SELECT $wpdb->users.ID FROM $wpdb->users WHERE (SELECT $wpdb->usermeta.meta_value FROM $wpdb->usermeta WHERE $wpdb->usermeta.user_id = wp_users.ID AND $wpdb->usermeta.meta_key = 'wp_capabilities') LIKE '%administrator%'" );
return $ids;
}
Matt on November 1 11 / 304 Permalink
Hi Brent,
That’s great, thanks for letting me know about user level being deprecated. The code will come in very handy too!
Ben Daughtry on February 7 12 / 37 Permalink
Hi, I came across your site because I had the exact same problem to solve. My solution was very similar to yours, but I was hoping for a better way, because I really don’t like
running LIKEs on longtext fields, which could cause a performance hit for sites with large memberships.
One thing that might help, (I know this post is a couple months old, but) since the desired return value from your sql query is the user id, you can improve performance by eliminating your subquery because the usermeta table contains user_id.
This would make your function look like
2
3
4
5
6
7
global $wpdb;
$ids = $wpdb->get_result( "SELECT {$wpdb->usermeta}.user_id from {$wpdb->usermeta} where ({$wpdb->usermeta}.meta_key='wp_capabilities') and ({$wpdb->usermeta}.meta_value LIKE '%administrator%')" );
return $ids;
}