Table of content

How to prevent registration with certain email domains?

You can either allow or prevent registration with certain email domains using the nsl_registration_user_data filter.

Example

Disable registration with the gmail.com email domain:

Code

add_filter('nsl_registration_user_data', function ($user_data, $provider, $errors) {
    /**
     * List of banned domains. Multiple domains can be separated with commas e.g.: "gmail.com","yahoo.com"
     */
    $banned_domains = array("gmail.com");
    if (isset($user_data['email']) && !empty($user_data['email']) && is_email($user_data['email'])) {
        $email_parts = explode('@', $user_data['email']);
        if (in_array($email_parts[1], $banned_domains )) {
            /**
            * The error message.
            */
            $errors->add('invalid_email', '' . __('ERROR') . ': ' . __('Sorry, registration with this email domain is not allowed!'));
            return $user_data;
        }
    } else {
        $errors->add('invalid_email', '' . __('ERROR') . ': ' . __('Sorry, email is missing or invalid!'));
    }

    return $user_data;
}, 10, 3);

Available since: 3.0.20