September 14, 2021 Migrated snippet to the Snippet Library.
Want to ban users from entering
email@example.com and spamming your registration form? Want to only allow users with your companies email address to signup (i.e. firstname.lastname@example.org). 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
new GWEmailDomainControl(array( 'form_id' => 152, 'field_id' => 9, 'domains' => array( 'gmail.com', 'hotmail.com', 'test.com' ), 'mode' => 'limit', ));
field_idargument 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', ) );
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_idargument 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_idargument at all.
domains array required
An array of email domains that should be banned or limited to depending on what
modeyou 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
%sanywhere in the message to output the invalid domain.
mode string optional
By default, this is set to
banwhich will mark any of the specified domains as invalid if they are submitted in the email field. You can also pass a
limitmode. In this mode, the specified domains are the only valid domains. All other domains will be marked as invalid.