Filter Disposable Email Addresses

The WS Form wsf_action_email_email_validate filter can be used to analyze and reject unwanted email addresses from form submissions.

The Filter

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 code would typically be added to your themes functions.php file, preferably the 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 ...
    );
}