How to get user by specific id in wordpress?

Member

by deron , in category: PHP CMS , a year ago

How to get user by specific id in wordpress?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , a year ago

@deron 

To get a user by a specific ID in WordPress, you can use the get_user_by() function. Here's an example of how to use it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$user = get_user_by( 'ID', $user_id );

if ( $user ) {
    // User found, you can access the user information
    $username = $user->user_login;
    $email = $user->user_email;
    // and so on...
} else {
    // User not found
}


In the above example, replace $user_id with the specific ID of the user you want to retrieve. This function searches for a user using the specified ID and returns an object containing the user's information.


Make sure to check if the user exists before accessing its properties to avoid errors.