email_exists()
Checks whether the given email exists.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Uses: get_user_by()
Hooks from the function
Return
Int|false
. The user ID on success, false on failure.
Usage
email_exists( $email );
- $email(string) (required)
- The email to check for existence.
Examples
#1 Checking a user's email when registering
If the email exists, it will display the ID of the user registered with that email. Otherwise, it will print the message that the entered email address is free and the user can be registered.
$email = '[email protected]'; $email_exists = email_exists( $email ); if( $email_exists ){ echo "This email is registered to user ID: {$email_exists}"; } else { echo 'You can use this email to register, it does not belong to any user'; }
Changelog
Since 2.1.0 | Introduced. |
email_exists() email exists code WP 6.8
function email_exists( $email ) { $user = get_user_by( 'email', $email ); if ( $user ) { $user_id = $user->ID; } else { $user_id = false; } /** * Filters whether the given email exists. * * @since 5.6.0 * * @param int|false $user_id The user ID associated with the email, * or false if the email does not exist. * @param string $email The email to check for existence. */ return apply_filters( 'email_exists', $user_id, $email ); }