DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

If you allow userregistration on your WordPress blog or website, what about displaying the 5 latest registered users? Here is a simple and efficient recipe to do so.

Simply paste the code below where you want to display recently registered users. This code will display 5 users, you can change this number on line 2.

<ul class="recently-user">
    <?php $usernames = $wpdb->get_results("SELECT user_nicename, user_url FROM $wpdb->users ORDER BY ID DESC LIMIT 5");
        foreach ($usernames as $username) {
                echo '<li>' .get_avatar($username->comment_author_email, 45).'<a href="'.$username->user_url.'">'.$username->user_nicename."</a></li>";

Thanks to emoticode for the snippet!!

One Comment

  1. You must use $wpdb->prepare function. Otherwise your code is open to hack.

    here is an example ,

    $usernames = $wpdb->get_results( $wpdb->prepare(“SELECT user_nicename, user_url FROM $wpdb->users ORDER BY ID DESC LIMIT 5”));

Leave a Reply to Dhanuka Cancel

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