fbpx
hacker, hacking, cyber security

How to Block Email Addresses in WordPress

Here is a solution to Block Disposable Email Addresses in WordPress in order to resolve registration spam.

Here is a solution to only allowed selected domains to register on your WordPress website.

// Allow Registration Only from @ email addresses

function is_valid_email_domain($login, $email, $errors ){
$valid_email_domains = array(“gmail.com”,”yahoo.com”,”hotmail.com”);// allowed domains change as needed
$valid = false; // sets default validation to false
foreach( $valid_email_domains as $d ){
$d_length = strlen( $d );
$current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
if( $current_email_domain == strtolower($d) ){
$valid = true;
break;
}
}
// Return error message for invalid domains
if( $valid === false ){

$errors->add(‘domain_whitelist_error’,__( ‘<strong>ERROR</strong>: Registration is only allowed from selected approved domains. If you think you are seeing this in error, please contact us.’ ));
}
}
add_action(‘register_post’, ‘is_valid_email_domain’,10,3 );

Place this code in your theme’s functions file – preferrable in a child theme you created or else you will need to update it with each theme update.
See it in action on this site.

Need some help with WordPress registration spam?  Contact us.