Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Randomly Display Registered Users in WordPress

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

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.

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 with WPCode Plugin

It’s common to find WordPress tutorials with instructions to add code snippets to your theme’s functions.php file, in a site-specific plugin.

However, the problem is that even the tiniest mistake in the custom code snippet can disrupt how your site functions. For example, if there’s an issue in the code, it can make your site inaccessible.

Instead, we recommend using the WPCode plugin. WPCode is an easy to use, beginner-friendly plugin that lets you safely add code to any part of your WordPress website.

It keeps all of your codes in one place and prevents errors that would occur if you were to edit your theme files manually.

That said, you’ll want to install and activate the WPCode plugin. If you need help, read our guide on how to install a WordPress plugin.

Upon activation, you can go to ‘Code Snippets’ >> ‘+ ‘Add Snippet.’

Code snippets add snippet

You’ll be sent to the snippet library where you can use from a range of code snippets.

However, in this case, we’ll click on ‘Add Your Custom Code’ to add our unique code snippet to our website.

Add your custom code WPCode

Name the title for your snippet such as ‘Random users.’ Make sure you change the ‘Code Type’ to ‘PHP Snippet.’

Random users code snippet

Add the following code snippet in the ‘Code Preview’ box:

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

    return $randomusers;
}

add_shortcode( 'randomusers', 'wpb_random_users' );

Here’s what it should look like in WPCode.

Randon users code preview

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.

After you’ve added the code snippet, toggle the ‘Inactive’ switch to ‘Active.’ Now click on the ‘Save Snippet’ button.

Save custom snippet

Once saved, scroll down to ‘Insert Method’ and click on ‘Shortcode.’ Copy the shortcode that WPCode has generated.

Copy code snippet

Next we’ll need to add the shortcode to our side menu or any other area 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:

[randomusers]

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 next to ‘Widget.’

Plus block icon

You need to find and select the ‘Shortcode’ block and drag it onto your sidebar or other widget area.

Shortcode in widget

After that, you need to paste the shortcode you copied above from WPCode into the ‘Shortcode’ box.

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

Paste code snippet

You can follow the same steps by adding the shortcode to a post or page. Make sure to read our guide on how to add custom code in WordPress, so you can better understand WPCode.

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. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

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

17 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. oleksandr says

    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 ?

  3. Javier S says

    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?

    Thanks

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

  5. WPBeginner Staff says

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

  6. 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: https://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.

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

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

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

      Admin

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

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

WPBeginner Assistant
How can I help you?

By chatting, you consent to this chat being stored according to our privacy policy and your email will be added to receive weekly WordPress tutorials from WPBeginner.