How to List the Last Five Registered Users in WordPress

Home » Snippets » How to List the Last Five Registered Users in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Understanding user registration trends can bring valuable insights for your WordPress website. It can help you to understand your audience better, plan targeted content, and devise effective marketing strategies. One of the simplest yet essential metrics is identifying the last five registered users. In this article, we will guide you through the process of how to list the last five registered users on your WordPress site.

					function wpturbo_last_five_registered_users() {
    $args = array(
        'order'      => 'DESC',
        'number'     => 5,
        'orderby'    => 'registered',
    );

    $users = get_users( $args );

    echo '<ul>';

    foreach ( $users as $user ) {
        echo '<li>' . esc_html( $user->display_name ) . '</li>';
    }

    echo '</ul>';
}
add_action( 'init', 'wpturbo_last_five_registered_users' );
				

Before delving into the snippet, let’s first understand that this code is designed to list down the last five users registered on the WordPress website. It will list these users in descending order, meaning the latest registered user will be displayed first.

The function wpturbo_last_five_registered_users() starts by defining an array $args. This argument array is used to specify the criteria for the get_users() function, which retrieves the user data.

$args = array(
    'order'      => 'DESC',
    'number'     => 5,
    'orderby'    => 'registered',
);

Within this array, there are three key pairs:

  • ‘order’ => ‘DESC’: This tells the function to retrieve the users in descending order.
  • ‘number’ => 5: This limits the number of retrieved user data to 5, thus ensuring we only get our last five users.
  • ‘orderby’ => ‘registered’: This tells the function to sort/order the queried users by the date of registration.

Next, the $users variable is declared and set to hold the return result of the get_users() function when passed the $args array.

$users = get_users( $args );

An unordered list is then started with the echo '<ul>'; line.

What follows is a foreach loop that iterates over each User object stored in the $users array. For each User, it prints a list item with the display name of the user.

foreach ( $users as $user ) {
    echo '<li>' . esc_html( $user->display_name ) . '</li>';
}

It’s important to note the esc_html() function here. This escapes any HTML potentially harmful or disruptive that might be stored in $user->display_name, thus safeguarding the site’s integrity.

The function then closes the unordered list with echo '</ul>';.

Finally, we connect our newly defined function wpturbo_last_five_registered_users() to the init hook using the add_action() function.

add_action( 'init', 'wpturbo_last_five_registered_users' );

The init hook is typically run just after WordPress has finished loading, but before any headers are sent. This renders our function and hence our user list during the initialization stages when a page is being loaded.

Register an account to save your snippets or go Pro to get more features.