Recently for a project it was necessary to restrict WordPress registration to a select few specific domains, for example: domains with an email address from a certain company, i.e. email addresses ending with @warrenchandler.com.
Rather than using a plugin to do limit domain access when registering, a few lines of code is all it takes to restrict access on sign-up, with a handy error message included to boot.
Here’s the code you’ll need – added to your site theme’s functions.php file.
// Allow Registration Only from @warrenchandler.com email addresses function is_valid_email_domain($login, $email, $errors ){ $valid_email_domains = array("warrenchandler.com","warrenchandler.co.uk");// allowed domains $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 the system administrator.' )); } } add_action('register_post', 'is_valid_email_domain',10,3 );
And that’s all it takes. Invalid email domains will produce the following result:
Hi Warren,
There is an error found please find below.
Your PHP code changes were rolled back due to an error on line 179 of file wp-content/themes/astra/functions.php. Please fix and try saving again.
syntax error, unexpected ‘&’
Thanks Raakesh. The issue was a bad paste which converted the HTML code when using the < pre > tag. I’ve updated it.
Awesome, thx Warren!
Hi Warren. How r u?
Does the code work with Profile Builder plugin?
I just writted it on my DIVI Child and nothing happens. Maybe because of the Profile Builder I guess.
Regards,
Vini.
Hi Vini,
That function hooks into the native WordPress register function. It’ll work fine in most cases, but if Profile Builder is taking away the WordPress functions and using its own, it won’t work unless you find the corresponding hook and modify it to suit your needs.
Thanks for stopping by and Happy New Year!
Hi, could I use some modified version of this code to restrict access to a list of pre-made codes? I don’t know php (just CSS, HTML and basic JS), maybe what I write doesn’t make sense at all. So, to have something like:
function is_valid_registration_invitation_code ($username, $invitation_code, $validation_errors ){
$valid_registration_invitation_code = array(“code”,”code”);// – I would need to add a list of up to 200 codes. Would it be ok if I have so many in array? I know database upload would be better, but that makes things even more complicated for me.
…And to just change in the rest of the code valid_email_domains to valid_registration_invitation_code. Can text after valid be anything I choose to set? If I add in registration form a field named ivitation_code, can I then use it for validation instead of email?
What I am trying to achieve is to add a field for invitation code in WooCoomerce registration and validate it. (I found this code for email domain adapted to Woocommerce on Stack Overflow) It would be great if I can set that the code can be used only one time (I know it is possible, saw some discussions but they all require setting up a database and make me even more confused).
If you know how this can be done in WooCommerce, can you at least point me in the right direction, where to look for tutorial because I tried researching over 3 days and from every tutorial and code I found I am missing a piece to adapt it (because I don’t have a knowledge of php and probably I don’t even know how to phrase a search to get what I am looking for).
Hi Vanda, the only limit to an array is the memory of the server, so you should be okay. You can easily add the list to a database, but you’d then need a database query which would still output to an array. The simplest way is to add the list in notepad++ or similar, and create a text array as I’ve described. That’s how I would do it if I was unsure of what I was doing :)
Hi,
I’m trying following same for roles. For example ‘ROLE1’ can register with any email address, but ‘ROLE2 is limited to, lets say .edu accounts.
Reference: https://wordpress.stackexchange.com/questions/248123/restrict-certain-roles-registrations-by-domain
When I try this snippet (answer: stackexchange)- it says “your site is experiencing a technical issue”
Can your code be modified for different roles like my question above?
Thanks
It can, but if you see that message, the first place I would start would be with your error.log. See what the “technical issue” is and you may find it’s simply a missed semi-colon or something simple like that with malformed code.
Hi Warren,
I’m trying to use this method on my web page, but was wondering if there is a way to add it on a specific page only.
eg, all clients can register on page A
the rest can register on page B but their domain HAS to have domain.co.za in their email address?
Hi Jax,
Let’s just say (to give an example) you have a WordPress page named “Sign Up” with the ID of 99 and the slug of ‘sign-up’.
You could just wrap the relevant parts of the function in a simple if statement;
Hi,
How to restrict registration *from* Certain Email Domains?
Simply change the line:
if( $current_email_domain == strtolower($d) ){
TO:
if( $current_email_domain != strtolower($d) ){
This will do the opposite.
Hello. First of all, thank you, I have traveled the web far and wide and finally I have landed in the right place. The code works perfectly. I put it in the function.php of the DIVI theme and it goes perfectly when I use the wordpress form for registration. Unfortunately, however, I use an addon for the DIVI theme, which bypasses this code, allowing the registration even to domains not included among those indicated by me in the array. This addon does not have its own function.php.
Would you be kind enough to give me some advice on this? Forgive the length of the post. I thank you in advance.
For reference, DIVI typically has a functions.php file in /wp-content/themes/Divi/functions.php.
Hi Warren, what a lifesaver! thank you so much.
I am experiencing a similar issue to the user above but mine is related to woo commerce, how would I apply this code to even block woo commerce registractions.
Hello Warren, I already have a Theme on WordPress with no code experience. How do we connect a specific form to a code in WordPress? do you have a tutorial video that covers that? Thank you
There are plenty of plugins that easily embed forms that you can use. Gravity Forms is a great paid option.
I was able to quickly turn this into a way to disallow unwanted email domains (thanks, Warren!):
// Disallow registrations from unwanted email domains
function is_invalid_email_domain($login, $email, $errors ){
$invalid_email_domains = array(“mailingo.com”,”mailingo.one”,”mailingo.eu”);// disallowed domains
$valid = true; // 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 = false;
break;
}
}
// Return error message for invalid domains
if( $valid === false ){
$errors->add(‘domain_whitelist_error’,__( ‘ERROR: Registration is only allowed from selected approved domains. If you think you are seeing this in error, please contact the system administrator.’ ));
}
}
add_action(‘register_post’, ‘is_invalid_email_domain’,10,3 );