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!
View this code snippet on GitHub.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:
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:
To apply this functionality to ALL fields on a form, do not pass the field_id
argument at all.
Here is a full list of available options:
View this code snippet on GitHub.form_id
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 theform_id
argument at all.field_id
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 thefield_id
argument at all.domains
An array of email domains that should be banned or limited to depending on whatmode
you have set.validation_message
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
By default, this is set toban
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.