The is_email_address_unsafe() WordPress PHP function checks an email address against a list of banned domains.
Usage
is_email_address_unsafe( $user_email )
Example:
Input: is_email_address_unsafe('[email protected]')
Output: false
(if ‘example.com’ is not in the banned domains list)
Parameters
$user_email
(string) – The email address provided by the user at registration.
More information
See WordPress Developer Resources: is_email_address_unsafe
This function is used in the WordPress Multisite environment to check user registrations against the Banned Email Domains list at wp-admin/network/settings.php
. User creation at wp-admin/network/users.php
bypasses this check.
Examples
Block registration with banned email domain
Check if a user’s email address has a banned domain during registration and display an error message if it is banned.
function check_banned_email_domain( $result ) { $user_email = $result['user_email']; if ( is_email_address_unsafe( $user_email ) ) { $result['errors']->add( 'banned_email_domain', __( 'This email address is not allowed. Please use a different email address.' ) ); }
return $result; } add_filter( ‘wpmu_validate_user_signup’, ‘check_banned_email_domain’ );
Add custom banned domains to the existing list
Add custom banned domains to the existing list and use the is_email_address_unsafe() function to check if an email address has a banned domain.
function add_custom_banned_domains( $banned_domains ) { $custom_banned_domains = array( 'bad-domain.com', 'another-bad-domain.com' ); return array_merge( $banned_domains, $custom_banned_domains ); } add_filter( 'banned_email_domains', 'add_custom_banned_domains' );
// Check if an email address has a banned domain. $is_banned = is_email_address_unsafe( ‘[email protected]’ );
Remove a domain from the list of banned domains
Remove a specific domain from the list of banned domains.
function remove_banned_domain( $banned_domains ) { $domain_to_remove = 'domain-to-remove.com'; return array_diff( $banned_domains, array( $domain_to_remove ) ); } add_filter( 'banned_email_domains', 'remove_banned_domain' );
Check if an email address is banned during user registration in a custom plugin
Create a custom plugin that checks if an email address has a banned domain during user registration and displays an error message if it is banned.
// my-custom-plugin.php function my_custom_plugin_register_user( $user_login, $user_email, $errors ) { if ( is_email_address_unsafe( $user_email ) ) { $errors->add( 'banned_email_domain', __( 'This email address is not allowed. Please use a different email address.' ) ); } } add_action( 'register_post', 'my_custom_plugin_register_user', 10, 3 );
Log banned email domains during registration attempts
Log the email addresses with banned domains during registration attempts.
function log_banned_email_domains( $result ) { $user_email = $result['user_email']; if ( is_email_address_unsafe( $user_email ) ) { error_log( "Banned email domain registration attempt" ); } add_action( 'registration_errors', 'log_banned_email_domains', 10, 1 );