email_exists()
Checks if the specified email exists among registered users.
Uses: get_user_by()
Hooks from the function
Returns
Int|false.
- User ID - if the email already exists.
- false - if such an email is not yet in the database.
Usage
<?php
if( email_exists( $email ) ){
// ...
}
?>
- $email(string) (required)
- Email to check.
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.9
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 );
}