In WS Form, you can easily prevent WordPress form submissions based on the domain of the email address entered.
There are two ways of doing this:
- Using the Allow or Deny setting in Email fields.
- Use a filter hook.
Each method is explained in more detail below.
Using the Allow or Deny Setting in Email Fields
This method doesn’t require any coding and provides a method of either allowing or denying specific email addresses or email address patterns.
Here’s how to set that up:
- Edit the settings of an Email field by clicking the icon.
- Click on the Advanced tab.
- Under Allow or Deny, set Method to Deny.
- Click the add icon to add an email address you want to block.
- Click Save & Close.
If you want to block a specific email address, simply enter it into the Email Address setting.
If you want to block all email address of a certain domain, use the format: *@domain.com
Using a Filter Hook
For more complex needs, such as blocking a large list of domains, the wsf_action_email_email_validate
filter hook is ideal. This hook allows you to analyze and reject unwanted email addresses during form submissions.
The wsf_action_email_email_validate
filter is supplied with four parameters:
$valid
– True by default. Return this value in your function if the email validates.$email
– The email address being validated.$form_id
– The form ID the email address belongs to.$field_id
– The field ID the email address was entered into (This will befalse
for emails checked in actions).
Your filter hook function can return one of the following return values
- True – The email address is valid, no action will be taken by WS Form.
- False – WS Form will show a standard error message indicating the email address is invalid.
- String – WS Form will show the string you provide to indicate the email address is invalid.
Sample Code
This example shows how to create a filter hook function that blocks disposable domains. You can easily adapt this code to block any other domains you wish. This code would typically be added to your themes functions.php
file, preferably in a child theme.
// Add filter for disposable email addresses add_filter( 'wsf_action_email_email_validate', 'wsf_action_email_email_validate_disposable_domains', 10, 4 ); // Hook function for wsf_action_email_email_validate filter function wsf_action_email_email_validate_disposable_domains( $valid, $email, $form_id, $field_id ) { if( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { // Split email address by @ $email_array = explode( '@', $email ); // Extract domain $domain = array_pop( $email_array ); // Check if domain exists in list of disposable domains if( in_array( $domain, wsf_disposable_domains() ) ) { // Return an invalid feedback message to be shown on the field return __( 'The email address provided cannot be used' ); } } return $valid; } // Array of disposable domains function wsf_disposable_domains() { return array( '0-180.com', '0-30-24.com', '0-420.com', '0-900.com', '0-aa.com', '0-mail.com', '0-z.xyz' // Add other domains here ... ); }