Featuring registered users on your WordPress website can boost engagement and create a stronger sense of community. It’s a great way to recognize your members and show that you value their participation.
However, if your site has many users, then highlighting them all can be overwhelming. Displaying a select few randomly is more manageable and effective.
That said, randomly showcasing registered users can lead to another issue. If you’re not careful, you might show the same users too frequently and discourage others from feeling appreciated.
In this article, we will walk you through the steps to display a list of random registered users on your WordPress site. You will learn how to set it up easily and effectively while ensuring that all your members feel included.
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 a good idea 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 different random users each time someone visits your website.
In the following sections, we will show you how to randomly display users in WordPress. Here’s a quick look at all the steps we will cover:
- Adding Custom Code to Display Registered Users
- Displaying Random Registered Users in a Widget (For Classic Theme Users)
- Displaying Random Registered Users in a Post or Page (For Block Theme Users)
- Displaying Random Registered Users in a WordPress Template (Advanced)
- Bonus Tip: Showing Real-Time Online Users in WordPress
Let’s jump right in!
Adding Custom Code to Display Registered Users
To display a list of random users, you’ll first 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.
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 break your site. For example, an issue in the code can make your site inaccessible.
So, 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.
To learn more about this plugin, you can read our complete WPCode review.
The first thing to do here is to install and activate the free WPCode plugin. If you need help, you can read our guide on how to install a WordPress plugin.
Upon activation, you can go to Code Snippets » + Add Snippet from your WordPress admin area.
This will send you to the snippet library, where you can use a range of ready-made code snippets. In this case, we’ll click ‘Add Your Custom Code’ to add our unique code snippet to our website.
On the next screen, you’ll choose ‘PHP Snippet’ as your code type.
This will then take you to the code editor page.
From here, you can name your snippet, such as ‘Random User Display.’
After that, you can 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, HOUR_IN_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' );
Important: You must replace the text where it says ‘HOUR_IN_SECONDS’ with the number of seconds you want the cache to expire. For example, we set ours ‘3600.’ To learn more about caching, you can read our ultimate guide to boost WordPress speed & performance.
Here’s what it should look like in WPCode:
This will create a list of 5 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, you’ll want to toggle the ‘Inactive’ switch to ‘Active.’
Then, go ahead and click the ‘Save Snippet’ button.
Once saved, let’s scroll down to the ‘Insertion’ section.
Here, you’ll notice that there are 2 insert methods – ‘Auto Insert’ and ‘Shortcode.’
Since the code snippet you just added has assigned the [randomusers]
shortcode you can use on your posts, pages, and widgets, then you will leave the insert method as ‘Auto Insert.’
Next, we’ll need to add the [randomusers]
shortcode to our side menu or any other area to display the list of registered users. We will cover 2 different methods.
Displaying Random Registered Users in a 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 will add the list to a widget.
You’ll need to navigate to Appearance » Widgets and then click the ‘+’ block inserter icon next to ‘Widgets’.
This will open the Widgets customization area.
From here, you’ll want to find and select the ‘Shortcode’ block and drag it onto your sidebar or other widget area.
After that, you can enter the [randomusers]
shortcode from WPCode.
Don’t forget to click the ‘Update’ button to make your changes live.
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.
Displaying Random Registered Users in a Post or Page
If you use a block theme, then you might not have a sidebar feature. Instead, you can display your random registered users in a post or page.
The first step is to access the full-site editing feature, so let’s navigate to Appearance » Editor.
Once inside, you’ll see your theme’s home template.
To edit a page, let’s click on ‘Pages’ in the left-hand vertical navigation menu.
On the next screen, you should see all the pages you’ve created on your WordPress website, including published, drafted, and scheduled ones.
From here, simply choose the page you want to edit.
For example, we will add the random user custom code to our demo site’s ‘Sample Page’ page, so we’ll give the ‘Edit’ pencil icon a click.
This will open the page’s content editor.
In the content editor, click the ‘+’ block inserter button and then use the search bar to look for the ‘Shortcode’ block to add the custom code snippet.
Once the ‘Shortcode’ block is added to the editor, go ahead and enter the [randomusers]
shortcode.
The content editor will not show you the preview. But don’t worry; the shortcode works just fine.
So, you can close the preview tab and hit the ‘Save,’ ‘Update,’ or ‘Publish’ button in the content editor.
That’s it!
Now, if you visit the post or page, you’ll see that it has a dedicated section for displaying random registered users.
To learn more about the WordPress block theme editor, you can check out our beginner’s guide to full-site editing.
Displaying Random Registered Users in a WordPress Template
The third method is to add a template tag to your theme’s template files, such as sidebar.php
or footer.php
.
To learn more, you can see our WordPress template hierarchy cheat sheet, which will help you find the right theme template file.
Then, simply add the following template tag code snippet where you would like the list of registered users to be displayed:
<?php wpb_random_users(); ?>
Bonus Tip: Showing Real-Time Online Users in WordPress
Tracking online users in real time allows you to monitor your WordPress website’s activity and provides live updates on visitor behavior.
For instance, if you’re running a 24-hour flash sale, real-time tracking helps you see how well it’s performing and whether it’s attracting more visitors. It also shows the immediate impact of your social media promotions.
Observing user activity can help you find the best times to publish blog posts. You can also see which countries your most active users are from, allowing you to tailor your marketing campaigns accordingly.
Finally, showing the number of active users creates social proof, making your site appear popular and trustworthy. This can boost new visitors’ confidence in exploring your content, making purchases, or sharing their contact information.
For more details, you can check out our guide on how to show real-time online users in WordPress.
We hope this tutorial helped you learn how to randomly display registered users in WordPress. Next up, you may want to read our guides on how to moderate new user registrations and how to allow users to invite their friends to register in WordPress.
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.
Yunus Rahman
Thanks for the helpful tutorial on randomly displaying registered users in WordPress! I wanted to showcase my community members on my website, and this guide made it easy. WPBeginner, your tutorials always help me take my website to the next level. Keep sharing your knowledge.
oleksandr
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 ?
Javier S
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
KIM
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..
WPBeginner Staff
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
WPBeginner Staff
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
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.
darkeizer
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?
Beth
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.
WPBeginner Support
Please check out our article on how to stop spam registrations on your WordPress site.
Admin
Rachel Dreher
I have the same question. Where are those people registering? I don’t see a button or link on my site to be able to do that. Sorry if its a stupid question!
Madeline
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
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
Madeline Yau
Would this help google search?
Madeline
WPBeginner Support
Probably not.
Admin
Greg Whitehead
If you are to use this in your template files don’t you need to use an echo before the function call?
WPBeginner Support
No.
Admin