Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Randomly Display Registered Users in WordPress

Would you like to feature some of your registered users on your WordPress website?

Highlighting your users can create a sense of community and build membership. However, on a site with a lot of members, it makes sense just to show a few users at random.

In this article, we’ll show you how to display a list of random registered users on your WordPress site.

How to Randomly Display Registered Users in WordPress

Why Randomly Display Registered Users in WordPress?

If you run any type of WordPress website that allows for user registration, then you may want to highlight your registered users.

This is a valuable practice whether you’re running a multi-author blog, a membership site, an online course, a forum, or another type of multi-user site. Your users are the superstars of your site, and it’s healthy to acknowledge them.

There are many ways you can highlight users and authors on your site. For example, you can add an author info box and display recently registered users.

However, popular sites will find it difficult to include all their members in one list. You might like to display a different set of random users each time someone visits your website.

In this article, we will show you how to display a random list of registered users in WordPress.

How to Randomly Display Registered Users in WordPress

To display a list of random users, you’ll have to add code to your WordPress files. If you haven’t done this before, then see our guide on how to copy and paste code in WordPress.

Adding Code to Functions.php

The first step is to add the following code snippet to your theme’s functions.php file, in a site-specific plugin, or by using a code snippets plugin.

function wpb_random_users() {
    global $wpdb;

    if ( false === ( $usernames = get_transient( 'wpb_random_users_cache' ) ) ) {
        $usernames = $wpdb->get_results( "SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY RAND() LIMIT 5" );
        set_transient( 'wpb_random_users_cache', $usernames, REPLACE_WITH_SECONDS );

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

    // 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>";
    $randomusers .= '</ul>';

    return $randomusers;

add_shortcode( 'randomusers', 'wpb_random_users' );

Important: You must replace the text where it says ‘REPLACE_WITH_SECONDS’ with the number of seconds you want the cache to expire. We recommend setting this number to at least 300, which would expire the cache in 5 minutes (300 seconds). Less than that could seriously affect your WordPress website’s speed and performance.

This will create a list of five random users. You can have more or fewer users in the list by changing the number in ‘LIMIT 5’ on line 5.

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

Now you’ll need to display the list of registered users. We’ll cover two different methods.

Displaying Random Registered Users in a Post or Widget

This method is easier for beginners. It lets you display newly registered users by using the following shortcode:


You can paste the shortcode into a post, page, or widget where you wish to display the list of random registered users. For this tutorial, we’ll add the list to a widget.

You’ll need to navigate to Appearance » Widgets and then click the ‘Plus’ Block Inserter icon. You need to find the Shortcode block and drag it onto your sidebar or other widget area.

Adding a Shortcode Block to the Sidebar

After that, you should paste the shortcode you copied above into the shortcode box.

Don’t forget to click the ‘Update’ button to make your changes live.

You can follow the same steps by adding the shortcode to a post or page.

Now you can visit your website to view your list of random registered users. To adjust how they appear, you’ll need to add custom CSS to your website.

Listing Random Registered Users in a Widget

Displaying Random Registered Users in a WordPress Template

The second method is to add a template tag to your theme’s template files, such as sidebar.php or footer.php.

To learn more, see our WordPress template hierarchy cheat sheet to help you find the right theme template file.

Then, add the following template tag code snippet where you would like the list of registered users to be displayed:

<?php wpb_random_users(); ?> 

We hope this tutorial helped you learn how to randomly display registered users in WordPress. You may also want to see our guide on how to get a free SSL certificate, and our expert picks of the best business phone services for small businesses.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

16 CommentsLeave a Reply

  1. WPBEGINNER STAFF help plz
    i insert code, work fine.

    i saw “SELECT user_nicename, user_url, user_email

    How do make user_url – to go to profile ?

  2. HI,

    Pretty nice code! Thank You!

    I am using Buddypress and bbPress, and I wonder how I can edit the code to use “user_nicename” with the url of profile and not with the website url?


  3. 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..

  4. This is called spam registrations. Check our guide on >a href=””>how to stop spam registrations in WordPress

  5. 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.


    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.

    • 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.

  6. 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?

  7. 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.

  8. 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


    • 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.


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

Leave a Reply to Madeline Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.