Recently we were asked to build a member list for a client. At first we thought that a list with just the names of the members of the choir would suffice, but the client actually wanted quite a bit more information!
Apart from the names of the members the information per member should also have email address, home address, mobile phone number and date of birth. We suggested then also to add the gravatar image and if members don’t have one to add a default image.
Searching on the net for a solution for this, brings up with quite a few results, but none of them really worked well for us, because we had two more restrictions: the admin should be excluded and the members should be listed in alphabetical order and not on member-ID.
Finally we found the solution on Stephanie Leary’s SillyBean.net! Her solution shows the results in a table and also includes other stuff we didn’t need, so we adapted her code a bit to suit our specific needs.
<div class="member-list">
<!-- code adapted from fantastic sample of Stephanie Leary (http://sillybean.net/wordpress/templates/creating-a-user-directory-part-2-building-the-user-page-template/) -->
<?php
$blogusers = get_users_of_blog(); //get all registered users
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->user_id); //get data about each user as an object
// create a flat array with only the fields we need
$allusers[$user->ID] = array(
'last' => $user->last_name,
'first' => $user->first_name,
'nicename' => $user->user_nicename,
'mobile' => $user->mobile,
'email' => $user->user_email,
'address' => $user->address,
'bday' => $user->bday,
);
}
asort($allusers); // sort the users by last name
?>
<?php
foreach ($allusers as $auser) {
if ($auser['nicename'] != 'admin') { ?> //exclude the admin from the member list (change into what you use instead of admin)
<p>
<?php echo get_avatar( $auser['email'], $size = '75', $default = 'http://image.url' ); ?>
<?php echo $auser['first']." ".$auser['last']." - ".$auser['email']; ?><br/>
<?php echo $auser['address']; ?><br/>
<?php echo $auser['mobile']; ?><br/>
<?php echo $auser['bday']; ?></p>
<?php } } ?>
</div><!--end member_list class-->
If you drop the code above in a Page template and give it a specific name, then you can call this template from within your WordPress Pages Editor. Keep in mind that you probably need to change the ‘nicename’ on line 22, unless you (still) use ‘admin’; if you want to call a default image, you need to change the URL on line 24.
And of course you will need to Change the Contact Info in the Users Profile. Good that we recently wrote a WordPress Tip on how to accomplish that!





15 Responses to Creating a Member List
Pingback: WordPress Tip: Change Contact Info in User Profile | WP TIPS
Very useful. Creating a members list is a good idea. Thanks for sharing
Are you using a specific membership plugin with the member directory you created for this site? I see that you have 4 pages or categories under members login “rehearsals, rules-regulations, directory, & documents”.
Great post thanks!
Hi Bridget, thanks for your comment.
We are indeed using a specific membership plugin: Justin Tadlock’s Members. Lots of possibilities to add roles to different user groups!
Under the members login we have 4 pages that can also contain sub-pages.
thanks for the tutorial but am having trouble with paginations, I see 10 people per page, but I do not know how I can continue, can you help?
That’s strange indeed, it should show all members! Without being able to see your code, it’s quite impossible to say what could trigger this behaviour. Any chance that you can show it on pastebin or something?
hello and sorry for my English, I explained badly. I would like 10 users per page, but I can not pagination to custom page? virtually all users can write articles on my site and list of contributors is long. You have a solution?
ok, understand you now. You would have to find a way to include the call to pagination into the function. Maybe the Codex can help you further?
the problem is that they are expert in PHP and I do not know how …. would be really great if you wrote a tutorial about …
Is there a way to exclude two admins? I tried adding….
if ($auser['nicename'] != ‘admin’) && $auser['nicename'] != ‘admin2′)
…but it did not work
Have you tried to use || instead of &&? I also noticed that in your code above you use curly quotes instead of single quotes around admin, that could also be the problem…
Thanks. I was not very clear, but using && in my customized code the first admin nicename is excluded just as it was before I added,the second condition, it is the second one that is being ignored. I did try a double pipe || but when using it neither name is excluded. In a past similar situation, && has worked just fine. I can’t figure out why it does not work here. I must be forgetting something.
Paul, pipes are used for OR, ampersands are used for AND, so if you want to use it in a conditional statement, I still think you should use pipes.
Have you tried:
($auser['nicename'] != ‘admin’ || $auser['nicename'] != ‘admin2′)
Another solution can be as described here: http://www.mattvarone.com/wordpress/list-users/#comment-1501
A little bit late, but for future reference
Add the following code
‘id’ => $user->id,
and then in your if statement write
($auser['id'] != ’4′ && $auser['id'] != ’1′)
Of course replace the numbers with the users you wish to hide!
Great bit of PHP that is coming in handy, I’ll post a link to the project that is using this code once it has gone live.
For those who are looking at the above code, please note it can also be used in conjunction with custom user fields, to give your author cards thats extra little touch!