Skip to content

How To Display Random Registered Users In WordPress

Yesterday, I shared a snippet to show recently registered users on your multi-user WordPress website.¬†Here’s another snippet which will show random registered users in your WordPress website’s front-end. It’s also a great to get more users to join your site.

This snippet comes from the WPBeginner. Just add following snippet to your current theme’s functions.php file:

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');