How to Restrict Email Domains

Overview

Would you like to restrict certain email domains from your form? It may be necessary to block certain domains from your WPForms. In this tutorial, we’ll show you how to use a PHP snippet to create a whitelist of accepted domains so that all other domains will be blocked from submitting form entries on your site.

Setup

In order to create your whitelist, simply copy this code to your site.

/*
 * Whitelist email domains from your WPForms.
 *
 * @link https://wpforms.com/developers/how-to-restrict-email-domains/
 *
*/
function wpf_whitelist_domains( $field_id, $field_submit, $form_data ) {
    $domain          = substr( strrchr( $field_submit, "@" ), 1 );
    $whitelist       = array( 'icloud.com', 'me.com' );
    if( ! in_array( $domain, $whitelist ) ) { 
        wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Email domain not accepted!', 'wpforms' );
        return;
    }
}
add_action('wpforms_process_validate_email', 'wpf_whitelist_domains', 10, 3 );

Remember to update the array( 'icloud.com', 'me.com' ); to include the domains you want in your $whitelist.

With the PHP snippet added, you can now restrict certain email domains from your form.

And that’s all you need! You have now successfully restricted email domains from your form. Would you like to restrict the number of characters allowed in your text fields? Take a look at our tutorial on How to Limit the Number of Characters for a Text Field.

Filter Reference: wpforms_process_validate_email

FAQ

Q: How can I make the snippet blacklist by domain?

A: To disallow email domains, just change the code snippet.

function wpf_blacklist_domains( $field_id, $field_submit, $form_data ) {
    $domain          = substr( strrchr( $field_submit, "@" ), 1 );
    $blacklist       = array( 'yahoo.com', 'hotmail.com' );
    if( in_array( $domain, $blacklist ) ) { 
        wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'We apologize for any inconvenience, we are unable to accept emails from this domain.', 'wpforms' );
        return;
    }
}
add_action('wpforms_process_validate_email', 'wpf_blacklist_domains', 10, 3 );

Q: How can I target a specific form?

A: To target a form, you’ll just need to add your form ID to the beginning of the code snippet.

/*
 * Whitelist email domains from your WPForms.
 *
 * @link https://wpforms.com/developers/how-to-restrict-email-domains/
 *
*/
function wpf_whitelist_domains( $field_id, $field_submit, $form_data ) {

    //Snippet will only run on form ID 999
    if ( absint( $form_data['id'] ) !== 999 ) {
    return;
    }

    $domain          = substr( strrchr( $field_submit, "@" ), 1 );
    $whitelist       = array( 'icloud.com', 'me.com' );
    if( ! in_array( $domain, $whitelist ) ) { 
        wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Email domain not accepted!', 'wpforms' );
        return;
    }
}
add_action('wpforms_process_validate_email', 'wpf_whitelist_domains', 10, 3 );