Created
August 18, 2021 10:36
-
-
Save stuartduff/e02a4690ca34f7ead51e67e9d01228b8 to your computer and use it in GitHub Desktop.
WooCommerce Restrict registration to certain email domains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function is_valid_registration_email_domain( $username, $email, $validation_errors ){ | |
$valid_email_domains = array( 'gmail.com', 'yahoo.com' ); // Add allowed domains here | |
$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 ){ | |
$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 ); | |
} | |
} | |
add_action('woocommerce_register_post','is_valid_registration_email_domain', 10, 3 ); | |
add_action('register_post', 'is_valid_registration_email_domain', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment