Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Randomly Display Registered Users in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Randomly Display Registered Users in WordPress

Your users are the superstars of your multi-user WordPress site. There are many ways you can highlight users and authors on your site. Previously we showed you how to add an author info box, and how to display recently registered users. In this article, we will show you how to display a random list of registered users in WordPress.

Displaying registered users randomly in WordPress

First thing you need to do is copy and paste the following code in your theme’s functions.php file or in a site-specific plugin.


function wpb_random_users() { 

global $wpdb;

$randomusers = '<ul class="random-users">';

// Query database for users
$usernames = $wpdb->get_results("SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY RAND() LIMIT 5");

// Display users in a list
foreach ($usernames as $username) {

if (!$username->user_url) :

$randomusers .= '<li>' .get_avatar($username->user_email, 45) .$username->user_nicename."</li>";

else :

$randomusers .= '<li>' .get_avatar($username->user_email, 45).'<a href="'.$username->user_url.'">'.$username->user_nicename."</a></li>";

endif;
}
$randomusers .= '</ul>';

return $randomusers;  
}

add_shortcode('randomusers','wpb_random_users');

This code queries the WordPress users table in your database and selects a random row, then it outputs the results in a bulleted list with user’s avatar and name. If a user has provided the website URL in their profile, then it will link the user name to their website.

Next thing you need to do is display the list of registered users. To do this, all you need to do is add the following line of code in your theme file where you want the user list to be displayed (such as sidebar.php, footer.php etc).

<?php wpb_random_users(); ?> 

You can also display a list of random users from your site using this shortcode in a post, page, or a widget.

[randomusers]

We hope this article helped you display a random list of registered users on your WordPress site. If you were looking to display a list of your staff members, then you should check out this tutorial on how to create a staff list in WordPress.

If you have any questions or feedback, then please leave us a comment below. Also don’t forget to follow us on Twitter and join us on Google+


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. KIM says:

    ah, Its working now!
    I pasted to the code at the function.php in current theme.
    is this correct, right?

    when I pasted the code to function.php in root directory, It was not working..

  2. WPBeginner Staff says:

    This is called spam registrations. Check our guide on >a href=”http://www.wpbeginner.com/plugins/how-to-stop-spam-registrations-on-your-wordpress-membership-site/”>how to stop spam registrations in WordPress

  3. WPBeginner Staff says:

    WordPress has a built in user management system. By default when you install WordPress you add the first user which is the administrator. You can add more users to your site by clicking on Users menu item in the admin bar.

    See: http://www.wpbeginner.com/glossary/user-role/

    However, user registration is turned off by default. So if you would like users to be able to register on your site, then you would go to Settings -> General and scroll down to the registration section. There you can check the box next to ‘any one can register’ to open your site for user registration.

    • Rachel Dreher says:

      Thank you. I see that now. I do have it selected, and I’ve had a decent number of users added. I’m just not clear on how they are adding themselves.

  4. darkeizer says:

    Hi I want to know if there is a plugin that can let me display a list of people (name, last name, address etc) that are registered in my organization. Not on my site.
    I have a access database with this info.
    better said, can i host a access file in wordpress?

  5. Beth says:

    How do people register to become a registered user on my WordPress site? Many people register on my site but their emails do not appear to be genuine. Mostly the addresses look like gibberish and the username registered bears no resemblance to the email addresses. I have tried responding asking them to confirm their registration but there are no responses.

  6. Madeline says:

    Hello,

    I know this is out of topic but how can I turn off a under construction page so the website is live to the public. Any suggestions? Looking forward a favorable answer. Thank you

    Madeline

    • WPBeginner Support says:

      You can put a page back to draft. Edit the page and from the Publish Meta box, under the Status drop down menu, choose Draft. Your website will remain live but the page will become inaccessible. Once you are done with your changes you can publish the page.

  7. Madeline Yau says:

    Would this help google search?

    Madeline

  8. Greg Whitehead says:

    If you are to use this in your template files don’t you need to use an echo before the function call?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.