
Recently, I needed to get all users of a certain role to modify a Comments Widget to remove those. Since WordPress 3.1, WordPress has made this quite easy to do now.
function wps_get_users_of_role( $role ) {
$wp_user_search = new WP_User_Query( array( 'role' => $role ) );
return $wp_user_search->get_results();
}
This will give you an array that contains the following in an object:
<pre>stdClass Object
(
[ID] => 1
[user_login] => username
[user_pass] => serialized PASSWORD
[user_nicename] => mynicename
[user_email] => my@email.com
[user_url] => http://mydomain.com
[user_registered] => GMT Time Stamp
[user_activation_key] => Something funky!
[user_status] => 0
[display_name] => My Display Name
)</pre>
You can even set a predefined array and get only necessary information.
$args = array(); $args[0] = 'user_login'; $args[1] = 'user_nicename'; $args[2] = 'user_email'; $args[3] = 'user_url'; $wp_user_search = new WP_User_Query( array( 'role' => 'editor', 'fields' => $args ) ); $editors = $wp_user_search->get_results();
Here’s the result:
<pre>stdClass Object
(
[user_login] => wpsmith
[user_nicename] => wpsmith
[user_email] => travis@wpsmith.net
[user_url] =>
)</pre>


So, I have been rather lazy announcing this, but I am speaking at WordCamp Atlanta coming up on February 4th at 11am on Professional Rapid Web Development Using the Genesis Framework.










