Block certain domains registering in WooCommerce

542 views Asked by At

I'm trying to make a function in my functions.php for blocking certain domains from registering. I know there are some plugins who accomplish the same, but I just don't want to install plugins to make things easy.

I've tried the function below, but it seems not to work right. In this example I've used gmail.com and yahoo.com but there are some known domains who create massive spam.

add_action('woocommerce_register_post','is_invalid_registration_email_domain', 10, 3 );
function is_invalid_registration_email_domain( $username, $email, $validation_errors ){
$invalid_email_domains = array( 'gmail.com', 'yahoo.com' ); // Forbidden domains
$valid = false; // sets default validation to false
foreach( $invalid_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 ){
    $error_text = __( "<strong>ERROR</strong>: Registration is only allowed from selected approved domains. If you think you are seeing this in error, please contact the system administrator.", "woocommerce" );
    $validation_errors->add( 'domain_whitelist_error', $error_text );
}
}
1

There are 1 answers

1
Momondo On

I can see that part of your code is taken from this answer:

Limit Domain Registration on WooCommerce

Instead of using woocommerce_register_post you should try this hook: woocommerce_register_form_start

And see if that would work.