Using WordPress ‘get_user_id_from_string()’ PHP function

The get_user_id_from_string() WordPress PHP function retrieves a numeric user ID from either an email address or a login.

Usage

get_user_id_from_string( $email_or_login );

Custom example:

$user_id = get_user_id_from_string( '[email protected]' );
echo 'User ID: ' . $user_id;

Output: User ID: 42

Parameters

  • $email_or_login (string) – Required. Either an email address or a login.

More information

See WordPress Developer Resources: get_user_id_from_string()

Examples

Get User ID from Email Address

Retrieve a user ID from an email address and display it.

$email = '[email protected]';
$user_id = get_user_id_from_string( $email );
echo 'User ID for ' . $email . ': ' . $user_id;

Get User ID from Login

Retrieve a user ID from a login and display it.

$login = 'johndoe';
$user_id = get_user_id_from_string( $login );
echo 'User ID for ' . $login . ': ' . $user_id;

Check if User Exists by Email or Login

Verify whether a user exists by either their email address or login.

$email_or_login = '[email protected]';
$user_id = get_user_id_from_string( $email_or_login );

if ( $user_id ) {
    echo 'User exists with ID: ' . $user_id;
} else {
    echo 'No user found';
}

Get User’s Display Name

Retrieve a user’s display name using the user ID from their email address or login.

$email_or_login = '[email protected]';
$user_id = get_user_id_from_string( $email_or_login );
$user = get_userdata( $user_id );
echo 'Display Name: ' . $user->display_name;

Delete User by Email or Login

Remove a user from the WordPress database using their email address or login.

$email_or_login = '[email protected]';
$user_id = get_user_id_from_string( $email_or_login );

if ( $user_id ) {
    wp_delete_user( $user_id );
    echo 'User deleted';
} else {
    echo 'No user found';
}