WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How to Randomly Display Registered Users in WordPress

How to Randomly Display Registered Users in WordPress

Last updated on January 28th, 2014 by Editorial Staff
42 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
How to Randomly Display Registered Users in WordPress

Your users are the superstars of your multi-user WordPress site. There are many ways you can highlight users and authors on your site. Previously we showed you how to add an author info box, and how to display recently registered users. In this article, we will show you how to display a random list of registered users in WordPress.

Displaying registered users randomly in WordPress

First thing you need to do is copy and paste the following code in your theme’s functions.php file or in a site-specific plugin.


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 table in your database and selects a random row, then it outputs the results in a bulleted list with user’s avatar and name. If a user has provided the website URL in their profile, then it will link the user name to their website.

Next thing you need to do is display the list of registered users. To do this, all you need to do is add the following line of code in your theme file where you want the user list to be displayed (such as sidebar.php, footer.php etc).

<?php wpb_random_users(); ?> 

You can also display a list of random users from your site using this shortcode in a post, page, or a widget.

[randomusers]

We hope this article helped you display a random list of registered users on your WordPress site. If you were looking to display a list of your staff members, then you should check out this tutorial on how to create a staff list in WordPress.

If you have any questions or feedback, then please leave us a comment below. Also don’t forget to follow us on Twitter and join us on Google+

42 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

16 Comments

Leave a Reply
  1. oleksandr says:
    Apr 28, 2017 at 12:12 pm

    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 ?

    Reply
  2. Javier S says:
    Apr 3, 2017 at 2:45 pm

    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

    Reply
  3. KIM says:
    Oct 13, 2015 at 12:47 pm

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

    Reply
  4. WPBeginner Staff says:
    Jul 27, 2014 at 9:06 pm

    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

    Reply
  5. WPBeginner Staff says:
    Jul 8, 2014 at 1:36 pm

    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.

    Reply
    • Rachel Dreher says:
      Jul 8, 2014 at 1:57 pm

      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.

      Reply
  6. darkeizer says:
    Mar 26, 2014 at 6:27 am

    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?

    Reply
  7. Beth says:
    Feb 6, 2014 at 12:52 am

    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.

    Reply
    • WPBeginner Support says:
      Feb 6, 2014 at 9:58 pm

      Please check out our article on how to stop spam registrations on your WordPress site.

      Reply
      • Rachel Dreher says:
        Jul 8, 2014 at 12:48 pm

        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!

        Reply
  8. Madeline says:
    Feb 4, 2014 at 7:30 am

    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

    Reply
    • WPBeginner Support says:
      Feb 5, 2014 at 9:11 pm

      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.

      Reply
  9. Madeline Yau says:
    Feb 3, 2014 at 8:33 pm

    Would this help google search?

    Madeline

    Reply
    • WPBeginner Support says:
      Feb 6, 2014 at 10:00 pm

      Probably not.

      Reply
  10. Greg Whitehead says:
    Jan 31, 2014 at 3:28 pm

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

    Reply
    • WPBeginner Support says:
      Feb 1, 2014 at 1:57 pm

      No.

      Reply

Leave a Reply 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.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
PushEngage
PushEngage
Increase your website traffic & revenue with push notifications. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2021 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2021)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2021)
    • SiteGround Reviews from 4464 Users & Our Experts (2021)
    • Bluehost Review from Real Users + Performance Stats (2021)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2021 – Step by Step Guide
Deals & Coupons (view all)
SendinBlue Coupon Code
Sendinblue Coupon
Get Sendinblue, a powerful marketing automation toolkit for small businesses, for FREE.
InMotion Hosting
InMotion Hosting Coupon
Get an exclusive 50% off InMotion hosting plus a free domain.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).

Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
  • Growth Fund
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon
  • AIOSEO

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress Security by Sucuri.