Using WordPress ‘get_user_by_email()’ PHP function

The get_user_by_email() WordPress PHP function retrieves user information by their email address.

Usage

get_user_by_email($email);

Custom Example:

$user_email = '[email protected]';
$user_info = get_user_by_email($user_email);
echo 'User ID: ' . $user_info->ID;

Parameters

  • $email (string) – The user’s email address for which the information needs to be retrieved.

More information

See WordPress Developer Resources: get_user_by_email

Examples

Displaying the user’s display name

This example retrieves the user’s display name using their email address.

$user_email = '[email protected]';
$user_info = get_user_by_email($user_email);
echo 'Display Name: ' . $user_info->display_name;

Checking if a user exists by email

This example checks if a user exists with the given email address.

$user_email = '[email protected]';
$user_info = get_user_by_email($user_email);

if ($user_info) {
    echo 'User exists';
} else {
    echo 'User does not exist';
}

Displaying the user’s website

This example retrieves the user’s website URL using their email address.

$user_email = '[email protected]';
$user_info = get_user_by_email($user_email);
echo 'User Website: ' . $user_info->user_url;

Retrieving the user’s registration date

This example retrieves the user’s registration date using their email address.

$user_email = '[email protected]';
$user_info = get_user_by_email($user_email);
echo 'Registration Date: ' . $user_info->user_registered;

Displaying the user’s role

This example retrieves the user’s role using their email address.

$user_email = '[email protected]';
$user_info = get_user_by_email($user_email);
$user_roles = implode(', ', $user_info->roles);
echo 'User Role(s): ' . $user_roles;