Skip to content

Instantly share code, notes, and snippets.

@AchalJ
Created August 29, 2020 10:07
Show Gist options
  • Save AchalJ/df9ad37aea9f7282a0e416b983203e21 to your computer and use it in GitHub Desktop.
Save AchalJ/df9ad37aea9f7282a0e416b983203e21 to your computer and use it in GitHub Desktop.
WP Allow User Registration from specific domain emails
add_filter( 'pre_user_email', function( $email ) {
$allowed_domains = array(
'example.com',
'yoursite.com',
);
if ( ! is_email( $email ) ) {
return $email;
}
$user_domain = explode( '@', $email )[1];
if ( ! in_array( $user_domain, $allowed_domains ) ) {
wp_send_json_error( array(
'code' => 'invalid_domain',
'message' => 'Sorry, you are not allowed to register.',
) );
}
return $email;
}, 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment