Skip to content

[WordPress] How to Use PHP Program to List the User Data

Last Updated on 2022-05-15 by Clay

We may be need to get the user data when we developing a WordPress plugin, for example, check the current user has registered; or list the writer of the site on a page... etc., The application scenarios are endless.

Among them, it can be further divided into:

  • Obtaining the current user information
  • Obtaining the specific user information

If fact, both of them are very simple, and I will introduce them in turn.


Obtaining the current user information

The following code shows how to get user name, email and identity.

 // Get user info
$current_user = wp_get_current_user();
                    
echo 'Username: ' . $current_user->user_login . '<br>';
echo 'Email: ' . $current_user->user_email . '<br>';
echo 'User first name: ' . $current_user->user_firstname . '<br>';
echo 'User last name: ' . $current_user->user_lastname . '<br>';
echo 'User display name: ' . $current_user->display_name . '<br>';
echo 'User ID: ' . $current_user->ID . '<br>';
echo 'User Role: ' . json_encode($current_user->roles) . '<br>';


Output:

You will find that even if the information does not exist, no error will be reported, but a null value will be returned.


Obtaining the specific user information

Before we obtaining the specific user information, record a sample program to obtain all user information. The concept is to use get_users() to get all users first, and then catch the user ID to get the corresponds user information through get_userdata(). The following steps are exactly the same as for a single user to obtain information.

// Get all users info
$users = get_users( array( 'fields' => array( 'ID' ) ) );
                    
foreach ( $users as $user ) {
    $_user = get_userdata($user->ID);

    echo 'Username: ' . $_user->user_login . '<br>';
    echo 'Email: ' . $_user->user_email . '<br>';
    echo 'User first name: ' . $_user->user_firstname . '<br>';
    echo 'User last name: ' . $_user->user_lastname . '<br>';
    echo 'User display name: ' . $_user->display_name . '<br>';
    echo 'User ID: ' . $_user->ID . '<br>';
    echo 'User Role: ' . json_encode($_user->roles) . '<br><br>';
}


Output:

As you can see, there are some test user accounts I added for testing.

However, this example is to acquire all users at once? What if we need to get a specific user?

If you read the get_users() official document, you will find that the parameter $args we pass in is meaningful. (that is array( 'fields' => array( 'ID' )).

Suppose you want to search for subscribers-only member information, you can modify the code to:

$args = array(
    'role'    => 'Subscriber',
    'orderby' => 'last_name',
    'order'   => 'ASC'
);
$users = get_users( $args );


This way, only the subscriber's data will be sent back. So you can use name, ID or any information of the user you know, to match the user you want to find.

For more information, you can refer to the official document description I have attached below.


References


Read More

Leave a ReplyCancel reply

Exit mobile version