Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brocheafoin/9bf171cca8675d75c61f to your computer and use it in GitHub Desktop.
Save brocheafoin/9bf171cca8675d75c61f to your computer and use it in GitHub Desktop.
add_action( 'um_before_new_user_register', 'require_whitelisted_email_for_signup' );
function require_whitelisted_email_for_signup( $args ) {
$user_email = $args['user_email'];
$approved_domains = array( 'yahoo.com', 'gmail.com', 'hotmail.com' );
$email_approved = false;
foreach ( $approved_domains as $domain ) {
$prefixed_domain = '@' . $domain;
if ( str_ends_with( $user_email, $prefixed_domain ) ) {
$email_approved = true;
break;
}
}
if ( ! $email_approved ) {
exit( wp_safe_redirect( add_query_arg( 'err', 'blocked_domain' ) ) );
}
}
function str_ends_with( $haystack, $needle ) {
$needle_position = strripos( $haystack, $needle, - 1 ); // -1 = Look at the exact end
$needle_expected_position = strlen( $haystack ) - strlen( $needle ); // length of the haystack minus length of the needle = beginning of the needle in the haystack
return ( $needle_position === $needle_expected_position );
}
@maximejobin
Copy link

Loving it! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment