To deny registration for specific email domains, you can use the `registration_errors` filter. This filter allows you to add custom validation during the user registration process.
Here’s how you can deny registration for users with emails from the `mailbox.imailfree.cc` domain:
function deny_specific_email_domains($errors, $sanitized_user_login, $user_email) {
$banned_domains = array('mailbox.imailfree.cc');
$email_domain = substr(strrchr($user_email, "@"), 1); // Extract the domain part from the email
if (in_array($email_domain, $banned_domains)) {
$errors->add('invalid_email_domain', __('ERROR: Registration using emails from this domain is not allowed.'));
}
return $errors;
}
add_filter('registration_errors', 'deny_specific_email_domains', 10, 3);
What this code does:
1. The function `deny_specific_email_domains` is hooked into the user registration process.
2. Inside this function, we check if the domain part of the registering user’s email matches our banned domain (`mailbox.imailfree.cc`).
3. If there’s a match, an error is added to the `$errors` object, which will prevent the user registration and display the error message.
You can add this code to your theme’s `functions.php` or use it in a custom plugin. If you have other domains you’d like to ban in the future, simply add them to the `$banned_domains` array.