How to list all registered users on your wordpress site

In my latest project i was required to add a community touch to a site by listing the Name, Email, Nickname, URI and Gravatar of all the registered users on the site. I came with the following solution : I made a new page template called users and added the following lines of php to the template :

<?php
/*
        First we set how we’ll want to sort the user list.
        * ID – User ID number.
        * user_login – User Login name.
        * user_nicename – User Nice name ( nice version of login name ).
        * user_email – User Email Address.
        * user_url – User Website URL.
        * user_registered – User Registration date.
*/

$sort= "user_registered";

//the default avatar to display in case gravatar is not available for a user
$default = "http://www.somewhere.com/some-directory/some-image.png";

//the size of the gravatar , here 96px
$size = 96;

//Build the custom database query to fetch all user IDs
$all_users_id = $wpdb->get_col( $wpdb->prepare(
        "SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY %s ASC"
        , $sort ));

//we got all the IDs, now loop through them to get individual IDs
foreach ( $all_users_id as $i_users_id ) :

// get user info by calling get_userdata() on each id
$user = get_userdata( $i_users_id );

//GETTING INFO FROM EACH USERS
//get the user’s email ID
$email = $user->user_email;

//build the gravatar URL
$grav_url = "http://www.gravatar.com/avatar.php?
gravatar_id="
.md5( strtolower($email) ).
"&default=".urlencode($default).
"&size=".$size;

//get the user’s full name
$user_fullname=$user->first_name . ‘ ‘ . $user->last_name;

//get the user’s URI
$user_url=$user->user_url;

//get the user’s nickname
$user_nickname=$user->nickname;

//get the user’s description ( the biographical info field, )
$user_profile=$user->description;

?>

<!– Now here you can display each users info using the variables defined above –>

<?php
endforeach; // end the users loop.
?>

Hope you found it useful, cheers :) , WORDPRESS ROCKS

2 Responses to this entry

Leave a Reply