Ban/Limit Email Domains for Gravity Form Email Fields
This snippet allows you to ban or limit any email domain(s) for your Gravity Form email fields.
September 14, 2021 Migrated snippet to the Snippet Library.
September 14, 2021 Migrated snippet to the Snippet Library.
September 14, 2021 Migrated snippet to the Snippet Library.
September 14, 2021 Migrated snippet to the Snippet Library.
September 14, 2021 Migrated snippet to the Snippet Library.
September 14, 2021 Migrated snippet to the Snippet Library.
Want to ban users from entering test@test.com
and spamming your registration form? Want to only allow users with your companies email address to signup (i.e. jane@yourcompany.com). This snippet will let you do both!
How do I install this snippet?
Easy peasy. Just copy and paste the code above into your theme's functions.php file.
How do I use this functionality?
All you need to do is create a new instance of the GWEmailDomainControl()
class, populated with your specific details, like so:
new GWEmailDomainControl(array(
'form_id' => 152,
'field_id' => 9,
'domains' => array( 'gmail.com', 'hotmail.com', 'test.com' ),
));
By default, the form will “ban” the specified email domains; however, you can also limit by the specified domains by passing limit
for the mode
argument. The example below demonstrates this as well as how to apply the email domain control to two specific fields by passing an array of field IDs for the field_id
argument:
new GWEmailDomainControl(array(
'form_id' => 152,
'field_id' => 9,
'domains' => array( 'gmail.com', 'hotmail.com', 'test.com' ),
'mode' => 'limit',
));
field_id
argument at all.
new GWEmailDomainControl(array(
'form_id' => 152,
'domains' => array('gmail.com', 'hotmail.com', 'test.com')
));
Here is a full list of available options:
new GW_Email_Domain_Validator( array(
'form_id' => 326,
'field_id' => 1,
'domains' => array( 'gmail.com', 'hotmail.com', '.co.uk' ),
'validation_message' => __( 'Oh no! <strong>%s</strong> email accounts are not eligible for this form.' ),
'mode' => 'limit',
) );
Parameters
form_id integer required
The form ID of the form you would like to apply this functionality to. If you want to apply the the same email domain control to all forms, simply do not pass the
form_id
argument at all.field_id integer required
The field ID for which this email domain control should be applied. If you would like to apply the same control to multiple fields you can pass an array of field IDs:
'field_id' => array( 8, 12, 15 )
If you want this to apply to all fields on a form, simply do not pass the
field_id
argument at all.domains array required
An array of email domains that should be banned or limited to depending on what
mode
you have set.validation_message string optional
The message which will be displayed below the email field if the submitted email contains an invalid domain. You can use
%s
anywhere in the message to output the invalid domain.mode string optional
By default, this is set to
ban
which will mark any of the specified domains as invalid if they are submitted in the email field. You can also pass alimit
mode. In this mode, the specified domains are the only valid domains. All other domains will be marked as invalid.
Can you do this for domain suffixes, eg .ru?
Hi Paul,
It appears it also works for domain suffixes. The domain parameter of the configuration will be like so ‘domains’ => array( ‘.ru’, ‘.com’, ‘.net’ ).
Best,
Great snippet. Recently something has changed and this will help get things back to normal.
Glad to know the snippet is useful to you.
Best,
Hi there
Great snippet indeed,
In my use case, I have a select field, which get some wp-users, and the field value is the selected user email.
Then I have an email field in my form, and I would like to chek if the email domain in this field is the same domain than the select field value.
If yes, then some field are displayed, if no some other field are displayed.
Is there any way to acheive this with this snippet or any mean to do that anyhow ?
Thank’s
You could do this by combining GP Copy Cat with this snippet. The snippet adds support for comparing two field’s values to trigger conditional logic.
Basically, you’d add two hidden Text fields to your form and use Copy Cat to copy the domains. Then, you’ll use conditional logic to compare those copied values and trigger the conditional logic. You’ll need an additional snippet to modify Copy Cat’s copied values. If you’re a Gravity Perks customer, drop us a line and we’ll be happy to help.
Hopefully this can filter out that annoying Eric Jones guy who seems to spam every form that ever existed!
We hope it will help!
This is really useful, there are a few individual people (or bots or whatever) that incessantly spam me and it’s great that I can filter them out. THanks!
Hi Paul,
You’re welcome. Glad to know the snippet is useful to you.
Best,
Please forgive this question (I am not a developer), but is this script PHP or Javascript?
Hi Margaret,
This is a PHP snippet. Here’s documentation on how to install the snippet.
Best,
Hi great script , thank you
is there a way to have the validation error message show before the user presses submit? Maybe a listening action or listen to mutations as the email is typed?
Hi Tico,
Unfortunately, we do not have a live validation solution.
Best,
Ok thank you Bassah! Any ideas on modification approaches to the current code to achieve this or resources that i could reference?
Hi Tico,
Not at this time, unfortunately, but let us know if you find anything suitable so we can ping the team.
Best,
OK thanks
Would LOVE to see this as a Perk with easily configurable options in GF main settings to set domains or on a per-form / per email field basis. It would make implementing this so much easier! And you’d get paid for it!
Nice feature Rachel! I’ll escalate to our product manager and see what he thinks.
Love it, Rachel. 💯
Definitely something we’d like to pursue. We’ve made a few new hires in the last couple months and have plans to give some of our free snippets a good UI and bring them into the suite. 🙂
Hello, I’m receiving the following error message:
“The snippet has been deactivated due to an error on line 104: Cannot redeclare function str_ends_with.”
Has anyone else received this error message? Any ideas on how to fix it ?
Thanks!
Hi Margaret,
It seems like a configuration error since the snippet works locally. If you have an active Gravity Perks Pro license, you can get in touch via our support form so we dig into this.
Best,
I think we’d all benefit from a video on the backend and how to apply the code properly. I’m pretty well educated and even I’m not getting most of this. Why not have a huge H2 tag that says
HOW TO BAN!
and then another that says
HOW TO ALLOW
Even your plugin, like there’s nothing there at all.
Hi Echo,
Thanks for the feedback. We’ve started making usage videos for our snippet and Perks as mentioned here. Hopefully, there will be a video for this snippet soon. However, if you’re still experiencing issues setting this up, you can get in touch with us via our support form so we can assist you further.
Best,
Hello, is this still working?
I am recently getting an error:
The snippet has been deactivated due to an error on line : Cannot redeclare function str_ends_with.
I see that someone else has posted a comment regarding this recently as well.
Thanks
Hi Jay,
I’m unable to reproduce this when testing locally. If you have a Gravity Perks license, drop us a line and we’ll be happy to look into this with you.
Great work with this function.
Can it be extended to allow only fully email addresses such as: testuser@testemail.com
Thanks.
Hi Hayley,
If I understand correctly, you want to limit the form to only specific email addresses. If so, then this is not supported with this snippet. However, we have another snippet, which would work for what you’re trying to do. Please check out the documentation for the snippet and details of how it works. With this solution, you would store the email addresses as entries of another form and use that to set up the main form.
I hope this helps.
Best,
Hello,
Does not work for a few days!
Can not redeclary the STR_ENDS_WITH function.
Possible to correct please?
Thanks.
Hi Anderson,
When testing locally it seems to be working as expected.
If you have an active Gravity Perks License, you can get in touch with us via our support form with your account email address and we’ll be happy to dig into this further.
Best,
Hello! How I can multiple forms to form_id param?
Hey West, we have a handy tutorial written for this: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
TLDR; just create a new instance for each form. 🙂
Oh … finally figured out why I was getting a PHP fatal error every time I pasted the code into my functions.php file … I was copying and pasting the short snippet of code from where it says
“All you need to do is create a new instance of the GWEmailDomainControl() class, populated with your specific details, like so:”
But what I didn’t realise is that I had missed all the code hidden in the “Show Code” section!
Suggestion – could you make it clearer that there is a lot more code than what is shown in the post?
e.g. change this: How do I install this snippet? Easy peasy. Just copy and paste the code above into your theme’s functions.php file.
to this: How do I install this snippet? Easy peasy. Just click “show code” above, then copy and paste that code above into your theme’s functions.php file.
And change this: How do I use this functionality? All you need to do is create a new instance of the GWEmailDomainControl() class, populated with your specific details, like so:
To this:
How do I use this functionality? After you have inserted the code above into your functions.php file, all you need to do is create a new instance of the GWEmailDomainControl() class, populated with your specific details, like so:
Hi Josh,
Thanks for the feedback. I’ll forward your suggestion for a proposal to review and update the document.
Best
Is this possible to have a wildcard of subdomain?
new GW_Email_Domain_Validator( array( ‘form_id’ => 2, ‘field_id’ => 4, ‘domains’ => array( ‘*.domain.com.au’ ), ‘mode’ => ‘limit’ ) );
Hi Steve,
Subdomains like this “domain.com.au” are actually supported. If the mode is set to limit, the snippet will only allow email addresses within the subdomain specified in the domain array parameter but you will exclude the wildcard and dot. Something like this should work
new GW_Email_Domain_Validator( array( 'form_id' => 2, 'field_id' => 4, 'domains' => array( 'domain.com.au' ), 'mode' => 'limit' ) );
Best,
Does this work on domains containing two dots? We have students who are on ‘student.organisation.tld’.
Hi Filip,
It should work with domains containing two dots.
Best,
Hello. I’ve used the code successfully for a single form. I wanted to use the same functionality for 3 similar forms with 3 different IDs. I tried using an array for the declaration and if statements that required the form id, but the “banned emails” did not filter. What should i do instead? Thank you so much!
I was thinking on copying all the codes 3 times for each form ID but that sounds like a bit of a hassle.
Hi Ana,
You can create multiple instances of the snippet configuration to target 3 different forms. Here is an article on how to do that: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Best,
Hi Ana,
Did you find a solution? I have tried multiple variations on this and it seems to read/verify the last form entry.
Hi Carlos,
If you’re a Gravity Perks Subscriber, you can get in touch with us via our Support Form, with form exports and additional information on what you’re trying to achieve so we can assist you.
Best,
Is there a way to form this same function but instead for a single line text field?
Hi Mark,
That’s a great fit for GF Blacklist. You can use it to reject submissions for any field that matches a list of blocked words, phrases, or IPs.
So by just changing the field ID this function code will work with single text field inputs?
Hello Mark, You would need to change the form ID, Field ID and specify what domain you are looking to ban, then the functionality should work correctly for your use case.
this is brilliant, and thank you sincerely for it. I’ve created an admin form with a field in options table for a comma delimited list (e.g. domain.one, domain.two) – I want to use that list as my domains list. I have (via get_option) a string (domain.one, domain.two)… and in GWE array set ‘domains’ => $mystring. But it doesn’t work – ignore the domains entirely. I tried exploding the string to an arrray, still nothing. Any advice on how to get a string from WP_OPTIONS table to be the domains list? Even if not, thank you deeply for such a useful bit of code :)
Ok… I got it sorted… code in case it helps anyone.
https://snippi.com/s/yg7r42h
Cheers.
Hi,
The error is thrown when the user submits the form. Can we have the error displayed when the user navigates away from the field? That way we will have a better user experience and reduce the clicks for the customer and also the page reload will be saved too.
Hi Rupak,
That would have been nice but unfortunately, Gravity Forms doesn’t handle real-time validation by default. There are add-ons for Gravity Forms that add real-time validation, but they don’t typically support third-party field types.
I hope this helps.
Best,
Is there a way to edit the code to allow a specific email address, eg specificemail@gmail.com?
I have it locked down to only allow one domain but have need to add two specific emails that are not the same as the allowed domain.
Hi Daniel,
This should be possible with our GP Blacklist Perk. Please check it out.
If you’re having difficulties setting it up, you can send us a message via our support form and we’ll be happy to assist you further.
Best,
Great snippet – I’ve been using this for years but am now getting hit with a ton of spam from a wide variety of non-US TLDs….is it possible to enter a domain wildcard to ban all domains from a given TLD, something such as ‘*.ru’ ?
Hello Trisha, You should be able to do this by banning the domain like this: ‘domains’ => array( ‘.ru’ ); Hopefully that works for you.
Can I limit email address for multiple forms?! Like if u apply with one form u can’t apply for the other form with same Email address?!
Hello Othman, You can accomplish this by creating multiple instances of the snippet. More details here: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
How I can do the opposite in the domain option and have there only the emails allowed?
For example: I want to only accept emails from @mycompany.com
Any help will be much appreciated.
Hello Abel, We do have a snippet here that will do just that. Within the configuration you will want to change ban to limit so the form is limited to only emails from your companies emails.
passing more than one form ID only the first one validates, can I do multiple form IDs in the same script or do I need to a script for each??
THanks!
Hey Eric, you will need to create multiple instances of the snippet. More details here: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Great snippet! Can this be used to ban domains with different country extensions without having to list every one? (for example yahoo.com, yahoo.de, yahoo.ca etc.)
Thanks!
Jonathan
Hi, your demo is still live and working properly. I added the code through the download code button in my child theme’s functions.php, altered the form ID and field ID (tried e-mail field – single and with double confirmation – and a simple text field) but nothing seemed to work.
Is your demo using the latest version of GravityForms?
Hello Jeroen,
I have just tested this out on my own test site and was able to use the snippet. Can you drop us a support request to better troubleshoot this for you? Thank you so much! :)
Hi David,
Does this snippet only work if we’re running Gravity Perks ad-ons?
Thanks!
Hi Lindsey, no, this should work with just Gravity Forms. I setup a demo so you can see it working: https://demos.gravitywiz.com/?gplp=preview&id=101
This snippet seems to be blocking all email addresses since Gravity Forms Version 2.4.14
Hi Dale, I setup a demo to see if I could recreate this issue but was unable to: https://demos.gravitywiz.com/?gplp=preview&id=101 If you’re a Gravity Perks customer, we’ll be happy to dig in to your implementation via support.
Hello,
Thank you for this! The functionality is working as intended but somehow it won’t show the validate messages anymore on the fields. I’ve used the validation you’ve created:
validation_message’ => __( ‘Sorry, %s email accounts are not eligible for this form.’ ),
Hi James, just setup a demo to see if I could recreate this issue. No luck: https://demos.gravitywiz.com/?gplp=preview&id=101
Hi there, any way to adjust this so it’s not a whole domain, but just a specific email address?
That’d be a better fit for our Gravity Forms Blacklist plugin. Alternately, you could just add conditional logic to the Submit button so that it’s hidden if the undesirable email is entered.
Hi David! Thanks for this snippet! It does exactly what I want. One thing I noticed in testing though which maybe you could help with…When I entered a banned domain and submitted the form (located at the bottom of my page), the page reloaded and stayed at the top. So a user would not know their form didn’t get submitted unless they scrolled down. Any thoughts on this?
It sounds like your issue is simply that the page isn’t scrolling down for any validation error. See the docs for gform_confirmation_anchor for some suggestions on this. ?
Hey David! Great code and works well in Gravity Forms. I implemented it and it stops entries from populating in the Gravity Forms area.
Something odd: my HubSpot integration is still picking up that I have a new entry from test@test.com and sends me an email to notify me when I submit the form.
Any tips to stop this from happening? Thanks!
Strange. I’d expect HubSpot’s feeds to only be triggered when the form is submitted (like other feed add-ons). If enough users report this issue, we’ll dig in and see if it’s something we can address in the snippet.
I’m having having a similar issue with hubspot. the validation catches the flagged email but the submission is sent to hubspot
I’m having trouble reproducing this with the Gravity Forms Hubspot Add-On. If you have a Gravity Perks subscription, drop us a line and we’ll be happy to dig into this.
Great snippet! Thanks a lot!
Glad to help, Karolina!
Hi. Is this still functioning? or is it dependent on anything else? The forms just continue to submit, unrestricted.
Hi Efficiency, yes, this is still working. There’s a chance you may not have installed it correctly. Try these troubleshooting steps: https://gravitywiz.com/documentation/snippet-troubleshooting/
Great snippet! Thanks so much for sharing :)
Glad to help. ?
Hi,
I want to translate the validation message with Loco Translate but for some reason it won’t translate the message.
Someone who have the same problem or a solution?
Thanks!
Can I just add this to ban email domains on ALL forms?
new GWEmailDomainControl(array( ‘domains’ => array(‘gmail.com’, ‘hotmail.com’, ‘test.com’) ));
Hi Paul, yes, that should work. :)
Hi there,
Does the ‘form_id’ accept an array as well? I only want to ban certain email domains on two of my forms…
Hi Caitlin, you will need to create multiple instances of the snippet. More details here: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Can this also ban specific email addresses rather than an entire email domain. Let’s say I wanted to ban testemail@gmail.com but not all gmail.com addresses? Thanks!
This will not ban specific email addresses; however, you could do that with GP Blacklist. Just add the email as a blacklisted word and enable the blacklist on your email field.
I couldn’t get this code to work :/
Sorry to hear that, Navin. This might help: https://gravitywiz.com/documentation/snippet-troubleshooting/
Hey, thanks
Our pleasure. :)
I love you.
(Said in manliest way possible).
We ❤️ you too. ?
Hey,
Say I have 10 forms and i just want the validation to work on 5 of them, i tried array with “form_id” but it doesn’t seems to work. Any advice?
Does this snippet still work? I tried it and it doesn’t work for me.
Not sure any of my previous comments have gone anywhere. But I figured out the problem.
Glad to hear it, Dave. We try to respond to comments on the blog within a week. :)
Hi Dave, I’m having the same problem can you tell me how you got it to work?
I got ” Class ‘GWEmailDomainControl’ not found ” :(
Hi Charlie, this might help: https://gravitywiz.com/documentation/snippet-troubleshooting/
can you use this to ban .ru emails?
Yup!
Thanks David. So do I use an asterisk or something? Like @.ru? Just to be clear I want to ban all .ru emails. Thanks in advance?
It should just be something like:
'domains' => array( '.ru' );
Hi David,
great snippet, perfect to block mail.ru spammers sponsoring ‘SibCoin’ (russian bitcoin) – I’m receiving hundreds of these requests.
Do you have a similar snippet to block spam / bad words (e.g. ‘SibCoin’, …) instead of email domains?
Thanks
Hi Davide, no snippet, but we do have a plugin that does just that. :)
https://gravitywiz.com/documentation/gravity-forms-blacklist/
How do I implement this in functions.php?
This might help: https://gravitywiz.com/documentation/snippet-troubleshooting/
why this mess while we can use ‘gform_field_validation’ filter i am doing the same thing with in 8 lines of code
Glad you were able to find a better way that works for you, Aamir.
Could you provide the solution? Thanks!
For me, I noticed the two checks between lines 50-57 proceed onto the next field even when “email” and “field_id” conditions are met, preventing the final validation step of the domain(s).
commenting/removing these lines is a hotfix if all you’re concerned with are the field type and id, but perhaps a different approach that covers all conditions..?
Hi Mike, I’m not sure I follow? What are you trying to do and how is the current code failing?
Hi, thank you for your work. Can You help me please? I need to people only with gmail and hotmail account could subscribe. So I’ve used you code. But for some reason I have an error. “Parse error: syntax error, unexpected ”field_id” (T_CONSTANT_ENCAPSED_STRING), expecting ‘)’ in /var/www/html/wp-content/themes/charitas-wpl/functions.php on line 114″ But my field id is right. I’ve tried to delete string with field id, to apply it to all fields. But then: “Parse error: syntax error, unexpected ”domains” (T_CONSTANT_ENCAPSED_STRING), expecting ‘)’ in /var/www/html/wp-content/themes/charitas-wpl/functions.php on line 114″
Can you help me please? What’s wrong?
Hi Michael, try this: https://gravitywiz.com/documentation/snippet-troubleshooting/
Thank you for this article. I came upon it as I have come to discover that when people enter their email @ymail.com that the form does not get sent to notify me. I am not clear how to apply what you have here to fix this. I have tried testing out the form entering all types of @bmail @ fmail and all work but not @ymail is this something you can help me with?
Is your goal to ban @ymail.com email addresses? Can you share how you’ve configured the snippet (just the configuration at the bottom)?
I did not put the snippet in.
I noticed that when someone is using a ymail. It does not get sent to me. So I began looking to see how to fix or if this is a glitch. I then came across this post but I am not sure how to use it in a way to fix this issue.
I do not want to ban ymail. I want them to be sent to me.
Here is the whole code for the function.php
Hi Jeff, I don’t think this snippet is going to help you. It sounds like there is some sort of mail server issue. Perhaps your mail server is interpreting ymail.com addresses as spam?
I seem to be having issues getting it working. Does this NEED to be in the theme functions? I added the code in a plugin because i am in the process of cleaning up the clients site and the theme needs to be cleaned out to allow a child theme to be created.
When i add the basic code i can still register with banned email accounts.
Hi Jason, I took a look and there doesn’t appear to be anything that would require this to be included in your theme’s functions.php. I confirmed this snippet is still working for me locally. Possibly something here might help: https://gravitywiz.com/documentation/snippet-troubleshooting/
Can I use this code for multiple forms?
Yup, this will help: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/ :)
Awesome coding! This has been invaluable to several of my clients. Thank you!
Happy to hear it, Bill. :)
I was wondering if anyone knows of a list of the top 100 or so types of free email clients (like gmail, yahoo, ymail, aol and so on) so we could limit our requests from these bulk spammers?
Any links or anything would be greatly appreciated :)
Good question. I did a little research. If you’re trying to simply block free email services, here’s a good list to work from. If you want to specifically target spam email domains, this list appears to be very comprehensive and is updated weekly.
Does this code snippet apply to one specific email address (as opposed to blanketing a domain)? I have a specific person spamming my contact form daily and I just want to block that one email address.
Hi Britt, this does not currently support blocking a single email address. You could add conditional logic on your submit button though that hides the submit button if that specific email address is entered.
Aha that should do it! Thank you :)
If you’re using WordPress Multisite is there a way to make this only work on forms within a specific site? By using site id in the array maybe?
Hi Dan, you could try something like this: http://snippi.com/s/wm4h8m3
Thanks David. I’ll see if that works. Instead of putting this in a theme’s functions.php couldn’t it also be implemented as a plugin?
Absolutely. :)
I believe your code has a typo at line 95. I believe you were wanting to compare the arguments domain ($_domain) instead of the email domain in order to limit/ban all emails from a TLD.
Hey Brian, do you have a use-case of a domain that’s failing? I just tested on my local form and the code appears to be working as expected.
I was trying to ban a top level domain. The strpos function is looking for a “.” as the first character but referencing the domain within the form field but should reference the arguments array domain ($domain). I added the “” and it worked perfectly.
My underscores were stripped out of the last comment.
Aha! Thanks for following up. I’ve made the fix. :)
BRILLIANT! Fixed, thank you so much.
Hi,
This is a great snippet and worked as expected so thank you for providing this!
Since our launch we found that country India has to be excluded from the free domain validator as India Universities do not have university specific email addresses for students. So I created a conditional logic which opens a second email field when country India is selected. Because we pre-populate some fields, it is validating the original field now hidden.
What would be the best way to only validate when it is an active field or exclude Country of Academic institution: India?
Link: https://qlikid-qa.qlik.com/signin?returnURL=https://qlikacademicprogram.qlik.com/student-application-form Username: amgreen Password: Sense2016
Form fields: Country of academic institution: India has no restriction and changes the email field. but the validator still errors for the other countries.
Thanks for your support.
Simon
Give this updated version a try: https://gist.github.com/spivurno/4055158
David, it works perfectly now! That is insane, you solved that in a matter of minutes, Awesome to have your talent and put it to great use! It really is so much appreciated, I really can’t believe how quick it was fixed!
Simon
Hi David,
Sorry to be a pain, using this alongside : https://gist.github.com/spivurno/7029518 if I enter a banned email which prevents progressing then the fields to field snippet picks up the first entry made(banned email), not the corrected valid email. I wonder if this is because of multi-page form: https://qlikacademicprogram.qlik.com/student-application-form/
If you happen to be able to help that would be superb.
Thanks Simon
P.S. Submitting here also as the other page does not seem to have activity anymore.
Try using the “force” modifier on your merge tag: {Field Label:12:force}
Hello, Thanks for the code, but somehow I cannot get this working! Here is the url: http://livetiles.wpengine.com/blog/big-data-analysis-new-big-thing/ Here is the pastie: http://pastie.org/10920724 I’d really appreciate if you can help me :) Thanks
Are you trying to limit to only use the specified domains or ban the specified domains?
Wow thanks so much! Works perfectly!
Glad you found this useful, Steve. :)
I get the following error: Fatal error: Class ‘GWEmailDomainControl’ not found in public_html/wp-content/themes/Avada-Child-Theme/functions.php on line 1187
Hi David, did you include the full snippet and not just the bit that initializes it?
Has anyone done something like this built into the email address field and using something like https://github.com/mailgun/validator-demo that would be awesome!
Not yet but we’re excited about a new perk that is coming soon with support for this! It’s going to complement one of our other upcoming perks really well, GP Page Transitions. Stay in the loop!
Hi,
Thanks for this neat snippet! Unless there was something funky in my environment, I think you introduced a problem for single-page forms when you added support for multi-page forms. I kept getting a undefined function on line 50 ( self::get_source_page( $form[‘id’] ) ), but once I commented out lines 50- 53 it worked fine.
Thanks again!
Hi Casey, you are correct and this has been fixed. :)
Hello,
I have pasted the code, removed the double opening tag, changed the form and field id, as well as the validation message – but I’m getting a http error 500.
Here is my entire child theme php (small amount of other code at the top, followed by your code): http://pastie.org/10858704
Any ideas?
Thank you in advance.
Try again with the latest version of the snippet (above). It has been updated.
I have tried installing this snippet several times and I keep getting the following error
“Fatal error: Call to undefined method GW_Email_Domain_Validator::get_source_page() in /home/serverdirectorhere/public_html/sitenamehere/wp-content/themes/customthemename-2015-Theme/functions.php on line 119”
Any suggestions would be greatly appreciated, thank you!
This issue has been fixed.
This is such a great feature but sadly I can’t get it to work on my multi-page form. The form is set up so that the email is the last field on the third page. It is also dynamically populated via a url parameter, so that it is pre-populated.
So when a user is on the first page and click on the next button, the form throws an error and won’t let them proceed.
If you could point me in the right direction to fixing this I’d greatly appreciate it. Thanks in advance!
Issue fixed. Grab the latest version above. :)
Thank you David, fixed now.
Hi David,
I tried using the code above and when I press “Submit”, the page just hangs. It doesn’t validate nor pass any information to the system and I’m getting a functions.php error: “Parse error: syntax error, unexpected ‘)’ after pasting the code and modifying it (to limit specific domains).
Thanks a lot for this solution. I hope you can help us out as to what went wrong.
Site: delseyb2b.asia
Pastie here – http://pastie.org/10769455
Error on line 177
Cheers
Looks like you added an extra two closing parenthesis. I’ve highlighted the line to remove: http://pastie.org/10769455#173
Thanks David, fixed.
I have installed the code successfully as part of my functions.pho file on this staging site:
http://thinktank01.staging.wpengine.com/
However, it’s not working – I can still enter gmail and hotmail addresses in the Gravity forms.
I can’t see anything I might be doing wrong but obviously I don’t have something set up correctly.
Here is the code I have appended to the functions.php file:
[code removed]
Hi Huge, could you share a pastie of your code? The formatting of the comments makes it difficult to read.
Trying out this snippet, I get a fatal error that php can’t redeclare the GWEmailDomainControl class. I’ve followed the tutorials, tried everything that my limited knowledge of php will allow, but no dice on a fix.
Any ideas?
Hi Josh, this means you’re including the snippet multiple times, possibly in the same file. Try removing the snippet completely and installing with these instructions: https://gravitywiz.com/documentation/snippet-troubleshooting/
Thanks so much for the quick response. I had actually already included the class for another project a year ago and totally forgot it was being called! Oops.
Thanks again.
Aha! Glad you were able to get this sorted. :)
is there a way to ban one specific email address? i.e instead of gmail.com can we singularly ban joe@gmail.com?
Hi Pete, this does not support banning a specific email address. It might be something I’ll add support for in the future. For now, there are two ways I can see doing this without custom code (depending on your needs).
1 – Enable “No Duplicates” on the email field and manually submit an entry with the email you wish to ban. The “No Duplicates” restriction will prevent this email from being submitted again.
2 – Use GP Comment Blacklist. Add the email you want to ban as a blacklisted phrase and enable the blacklist validation for this field.
I’d like to append a value to another field when a banned email address is entered. Eg. add ‘banned ermail’ to input_1_3 as well as banned validation message. How do I do this? Thanks
Could you elaborate on what you’re trying to accomplish?
Sorry I wasn’t clear in my previous comment.
When a banned email is entered and the form is submitted, instead of showing a validation message, I still want the form to be submitted but append a value to a hidden field instead. The hidden field will have a default value of 1 but the value will be replaced with 2 if a banned email is entered. I can then use the value of this hidden field to create some conditional logic, so if value = 1 redirect to page a if value = 2 redirect to page b. Hope that makes sense!
Ah, ok. Unfortunately, this snippet won’t do that for you. It only bans or limits which email addresses can be submitted.
Hi This is my bottom code http://pastie.org/10122990 but that seem not working , I believe the new GWEmailDomainControl has been put in a wrong way, please verify
It looks like your initializing this twice for the same field with different parameters. I’d remove this bit (highlighted). If it’s still not working, can you describe how it is not working and provide a link where I can preview the form?
Oh that did the tweak , Thanks man
I’m having an issue with case sensitivity. If my domains array includes “gmail.com” and the form input value is “Gmail.com” then how can I be sure that the email is converted to lowercase before GWEMailDomainControl does its compare?
To fix the case sensitivity I’ve changed line 50 in the snippet:
$domain = $this->get_email_domain($field);
to this:
$domain = strtolower($this->get_email_domain($field));
Hi Janet, thanks. I’ve updated the snippet to also account for the scenario where the admin user may specify the email domains to limit in uppercase or lowercase as well. It will now lowercase both values before comparison.
Hey
Great concept – just wish I could use it. I tried using it on our website but unfortunately it gave me the white screen of death – eeek! I am using x theme with a child theme on top.
Any ideas why?
Hi Hannah, this is generally caused by installation issues. Here’s a guide to help with common problems:
https://gravitywiz.com/documentation/snippet-troubleshooting/
Just what I’m looking for! Thanks!
I’d like to ban all popular webmail domains – gmail, yahoo etc. Is it possible to add a wildcard instead having to add each suffix? so gmail.* for example?
Hi Stefan, currently this is not possible. You’d need to specify each domain manually.
Gidday David, thank you for this great little snippet.
I have added Gmail as an allowed domain, but the script also allows submissions from @xyzgmail.com. Is it possible for validation to be performed against the exact domain rather than a partial match?
Hi Adam, this was a bug. Fixed now. :)
Goodness, that was super fast! Many thanks for the updated snippet and everything you’re doing with Gravity Perks. Cheers again.
Glad to help, Adam!
Hi there David,
How do you only allow emails with a specific TLD (top level domain)?
For example, I only want the form to accept email addresses ending in “co.fr” or “co.uk”.
Is that possible? I posted a similar comment earlier but I don’t know what happened to it.
Thanks!
I’ve updated the snippet to support this. You’ll want to get the version directly from the source:
https://gist.github.com/spivurno/4055175
Just added “.co.uk” in the “domains” array.
Thank you very much. You’re a life saver!
Actually on more feature request:
For example, here is an example of an email address that I want to allow:
“sub2.sub1.domain.co.uk”
This snippet works perfectly, but how do I limit email address with at 1, 2 or 3 subdomains, and still limit the form to only accept email addresses ending in “co.uk” for example?
This request is pretty specific. I’m going to hold off and see if any other users need something like this. If so, I’ll be happy to take a look at adding support for it.
I see, alrights thanks, I’ll keep updated.
Hi David,
Thank you for this valuable and helpful feature! Are we free to use for commercial use in the purchased Gravity Forms plugin? Thanks.
Absolutely. Enjoy!
This is beautiful! Thank you for providing this very useful snippet. Do you have a donation link?
Glad to help, Alan. The best way to say thank you is picking up a copy of Gravity Perks. Will benefit you and me. If that is more than you’re able to donate, you can send any amount to david at gravitywiz dot com via PayPal. :)
I have followed the instructions and added this code to my functions.php. I have changed all instances of ban to limit as I want to allow only emails ending in mydomain.org to register. I have added this to the domains array. Unfortunately it still allows registration with any email address domain. Have I missed something? Is there something else that needs changing to make this work?
Please reply ASAP.
Thanks, Arpita
I sorted it out. Thank you for the code.
Thanks for this! I am trying to get this to work with the Gravity forms User Registration plugin.
I have changed your code line 26 from:
add_filter(“gform_validation{$form_filter}”, array($this, ‘validate’));
to:
add_filter(“gform_user_registration_validation($form_filter)”, array($this, ‘validate’));
When calling GWEmailDomainControl() class in the array data I am specifying the form and field id of the gravity forms form linked to the User Registration settings.
Has anyone had experience applying this to User Registration forms?
Thanks!
Hey David,
Does this class allow for a dynamic array of domains? I have a project where the domains need to be determined from within WordPress rather than so I created a custom taxonomy that features a custom meta field for domain names, however the class doesn’t seem to accept said values. Instead it returns the following error:
Warning: in_array() expects parameter 2 to be array, null given in /public_html/wp-content/themes/themename/inc/theme-functions.php on line 150
I’ve confirmed that it does work with a hardcoded array of domains, so I’m sure I didn’t make a mistake copying over your code and I’ve tested my function in a few templates and it returns values as expected. (So, I’m fairly certain I didn’t make a silly PHP mistake. Maybe you’ll see something that says otherwise.)
Anyway, here’s an example of the code I’m using to create the array of domains: http://pastie.org/private/x6llxgyfpku9yl0cxk6yq
Note: Removing the
else
statement fromget_brand_domains()
does not change the warning, which I found interesting.Any thoughts on this? Thank you so much!
Hi Jeremy, what output do you see if you
print_r( $whitelist );
(anywhere below line 26)? My guess is that you aren’t getting an array of terms and instead you’re getting the “null” return (line 21). This may be caused because custom taxonomies have not yet been initialized when the functions.php is being loaded. If this is the case, you can wrap the initialization code in a function triggered on the “init” action: http://pastie.org/private/bar2edkgewb86xhnzcmaAhh, you guessed correctly–it was due to the custom taxonomy not being initialized. So, not a silly PHP error, but a silly WordPress error. My apologies for bugging you but thank you so much for your time and expertise; both are very much appreciated.
Cheers!
My pleasure, Jeremy.
Hi David,
I need to create a similar function that allows me to block all domains from a particular TLD for example all .tk email addresses?
Is there anyway to edit your function to allow this?
Thanks for any help
Charlie
David
I’ve tries this on a current version of WP and GF but I get a white screen error. I’ve only changed the field IDs so not sure what the issue is. Any sugegstions?
this is not working with gf 1.8.
my bad, i picked text field instead of email
No worries. Thanks for following up.
Hi David,
Trying to use this snippet with GF 1.7.2 but it generates a fatal error. What am I missing? Any help would be much appreciated!
Here’s the code I’m trying to use in the functions.php file:
//ban spammy email domains from gravity forms new GWEmailDomainControl(array( ‘domains’ => array(’email.com’, ‘asdf.com’, ‘test.com’) ));
Also, it didn’t paste right in the comment. The “new” is on the next line after the comment.
Hi Jon, what is the fatal error you are getting?
Thanks for the email update Jon! For others with the same issue, Jon was able to resolve his issue by re-pasting the full snippet.
Any way to get this to ban a specific email (i.e. email@gmail.com) instead of all gmail accounts. I have a random gmail address sending an email through about 10 times a day but I don’t want to ban all gmail addresses.
Hi Tyler, here’s a non-obvious solution that might do the trick.
What will happen now is when the “email@gmail.com” email is entered in the Email field, our required Single Line Text field will be “enabled” via conditional logic (though it will still be invisible, hidden by the “gf_hidden” CSS class) so when the form is submitted, it will fail validation and the user will be unable to submit unless they enter a different email address.
Perfect! Thank you David! This is great for those that want to prevent spam from temporary email addresses; such as sharklasers.com, guerrillamail.com and 10minutemail.com domains.
Glad you like it Jason!
Thanks very much David :)
I have followed the instructions and added this code to my functions.php. I have changed all instances of ban to limit as I want to allow only emails ending in mydomain.org to register. I have added this to the domains array. Unfortunately it still allows registration with any email address domain. Have I missed something? Is there something else that needs changing to make this work?
Resolved via email. Thanks Colin. :)
Hi David,
Can this be done in another way :
Would be nice
Kind regards,
S
Great! Thank you!
Hi David,
Thank you for sharing this. Can you help me with the following?
On my website I have several forms. Some of them are only for business others for both business and personal use.
I would like to avoid gmail and hotmail on the “business forms”. So far no problem.
But we need to create several forms (+260 forms) and I would like to avoid to change the functions.php every time a form created. Is it possible to address your code directly in the form editor of GF? In that case blocking email domains for a certain form is just so easy.
Or in case the first is not possible… We have more “business forms” than “both business and personal forms”. So your snippet is actually the standard for most forms. Can we instead of adding form IDs ‘form_id’ => 152, 153,… using negative form IDs to exclude them ‘form_id’ => -9, -10
Thank you for your time and consideration.
Kind regards
Hi David,
Coming back to my question above… Still hope you have time to help…
Can you change your snippet in a way that an array can be used for the form_id.
Kind regards,
Y
You can apply this functionality to a set of form IDs, like so: http://snippi.com/s/9e3net4
This is gonna sound a bit stupid but how do I “create a new instance of the GWEmailDomainControl()class”?
Does that mean the second snippet gets pasted into functions.php too?
Hi Avinash, all you need to create a new instance of the GWEmailDomainControl() class is to use this initializer:
http://pastie.org/5695188
You can create as many initializations of the GWEmailDomainControl() class as you need. :)
Hey David you are awesome! I’m still a PHP noob but I’m guessing from the code that I can simply declare more
new GWEmailDomainControl
to apply the validation to a 2nd, 3rd etc form right?
Yup! :)