How to Allow Form Submissions by Domain Name

In WS Form, you can easily allow WordPress form submissions based on the domain of the email address entered.

There are two ways of doing this:

  1. Using the Allow or Deny setting in Email fields.
  2. 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:

  1. Edit the settings of an Email field by clicking the icon.
  2. Click on the Advanced tab.
  3. Under Allow or Deny, set Method to Allow.
  4. Click the add icon to add an email address you want to allow.
  5. Click Save & Close.

If you want to allow a specific email address, simply enter it into the Email Address setting.

If you want to allow all email addresses of a certain domain, use the format: *@domain.com

WS Form - Email Field - Advanced Settings - Allow or Deny

Using a Filter Hook

For more complex needs, such as allowing a large list of domains, the wsf_action_email_email_validate filter hook is ideal. This hook allows you to analyze and approve 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 be false 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 allows only specific domains. You can easily adapt this code to allow any other domains you wish. This code would typically be added to your theme’s functions.php file, preferably in a child theme.

// Add filter for allowing only specific email addresses
add_filter( 'wsf_action_email_email_validate', 'wsf_action_email_email_validate_allowed_domains', 10, 4 );

// Hook function for wsf_action_email_email_validate filter
function wsf_action_email_email_validate_allowed_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 allowed domains
        if( !in_array( $domain, wsf_allowed_domains() ) ) {

            // Return an invalid feedback message to be shown on the field
            return __( 'The email address provided is not from an allowed domain' );
        }
    }

    return $valid;
}

// Array of allowed domains
function wsf_allowed_domains() {

    return array(

        'example.com',
        'company.com',
        'trusted.com'

        // Add other domains here ...
    );
}