WP User Object

WP User Object

To get an user object you can use this function:

$user_id = 14;
$user = get_user_by( 'id', $user_id );

where the first parameter can be: id | ID | slug | email | login of the user, and second: real user ID, slug, email address, or login name.

// value of $user

WP_User Object (
    [data] => stdClass Object (
            [ID] => 14
            [user_login] => dalia.litay
            [user_pass] => $P$BJt9RgnC52...
            [user_nicename] => dalia-litaygmail-com
            [user_email] => dalia.litay@gmail.com
            [user_url] => https://gmail.com
            [user_registered] => 2021-09-09 07:03:19
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => Dalia Litay
        )

    [ID] => 14
    [caps] => array (
            [subscriber] => 1
        )

    [cap_key] => wp_capabilities
    [roles] => array (
            [0] => subscriber
        )

    [allcaps] => array (
            [read] => 1
            [level_0] => 1
            [subscriber] => 1
        )

    [filter] => 
    [site_id:WP_User:private] => 1
)

To get user data by user e-mail:

$user_email = 'dalia.litay@gmail.com';
$user = get_user_by( 'email', $user_email );
$user_id = $user->ID;
$user_info = get_userdata( $user_id );

$username = $user_info->user_login;
$user_name = $user_info->display_name;
$user_email = $user_info->user_email;
$first_name = $user_info->first_name;
$last_name  = $user_info->last_name;
$user_level = $user_info->user_level;

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.