Using WordPress ’email_exists’ PHP filter

The email_exists WordPress PHP filter checks if a given email address already exists in the WordPress database.

Usage

add_filter('email_exists', 'your_function_name', 10, 2);
function your_function_name($user_id, $email) {
    // Your custom code here
    return $user_id;
}

Parameters

  • $user_id (int|false) – The user ID associated with the email, or false if the email does not exist.
  • $email (string) – The email address to check for existence.

More information

See WordPress Developer Resources: email_exists

Examples

Block specific email domains

Disallow email addresses from specific domains during registration.

add_filter('email_exists', 'block_email_domains', 10, 2);
function block_email_domains($user_id, $email) {
    $blocked_domains = array('example.com', 'baddomain.com');

    $email_domain = substr(strrchr($email, "@"), 1);

    if (in_array($email_domain, $blocked_domains)) {
        return true; // Email is blocked, return as if it exists
    }

    return $user_id;
}

Allow multiple users with same email

Allow multiple users to share the same email address.

add_filter('email_exists', 'allow_shared_email', 10, 2);
function allow_shared_email($user_id, $email) {
    return false; // Always return false to allow shared email addresses
}

Log email existence checks

Log the email existence checks to a custom log file.

add_filter('email_exists', 'log_email_exists', 10, 2);
function log_email_exists($user_id, $email) {
    $log_file = 'email_exists_log.txt';
    $log_message = $email . ' - ' . ($user_id ? 'Exists' : 'Not Found') . PHP_EOL;
    file_put_contents($log_file, $log_message, FILE_APPEND);

    return $user_id;
}

Ignore case when checking email existence

Ignore the case of email addresses when checking for existence.

add_filter('email_exists', 'ignore_case_email_exists', 10, 2);
function ignore_case_email_exists($user_id, $email) {
    global $wpdb;

    $lower_email = strtolower($email);
    $user_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->users WHERE LOWER(user_email) = %s", $lower_email));

    return $user_id;
}

Replace email_exists with custom function

Replace the default email_exists function with a custom function.

add_filter('email_exists', 'custom_email_exists', 10, 2);
function custom_email_exists($user_id, $email) {
    // Your custom email exists function here
    // Return user ID if the email exists, false otherwise
    return $user_id;
}