How to Randomly Display Registered Users in WordPress

Your users are the superstars of your multi-user WordPress website. There are numerous methods you may spotlight users and authors in your website. Beforehand we confirmed you add an writer information field, and show not too long ago registered users. On this article, we’ll present you show a random listing of registered users in WordPress.

Very first thing you should do is copy and paste the next code in your theme’s features.php file or in a site-specific plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 desk in your database and selects a random row, then it outputs the leads to a bulleted listing with consumer’s avatar and title. If a consumer has offered the web site URL of their profile, then it’ll hyperlink the consumer title to their web site.

Subsequent factor you should do is show the listing of registered users. To do that, all you should do is add the next line of code in your theme file the place you need the consumer listing to be displayed (resembling sidebar.php, footer.php and so on).

1
<?php wpb_random_users(); ?>

You may as well show an inventory of random users out of your website utilizing this shortcode in a submit, web page, or a widget.

[randomusers]

We hope this text helped you show a random listing of registered users in your WordPress website. For those who had been trying to show an inventory of your employees members, then you must take a look at this tutorial on  create a employees listing in WordPress.

You May Also Like : 9 Useful Youtube Tips to Spice Up Your WordPress Site with Videos

If you liked this article, make sure to hit the bell for getting our latest updates and share this article with your friends in every social media platforms.

Leave a Comment

Your email address will not be published. Required fields are marked *