Require a Business Email in Contact Form 7
Asks visitors to use their work email instead of a free webmail address such as Gmail or Yahoo, which helps B2B sites get qualified leads.
Opens WPIDE on
Code
$wpide_cf7_free_providers = array( 'gmail.com', 'googlemail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'live.com', 'aol.com', 'icloud.com', 'gmx.com', 'proton.me', 'protonmail.com', 'mail.com', 'yandex.com' );
$wpide_cf7_business_fields = array(); // Field names to check, e.g. array( 'work-email' ). Empty = all email fields.
$wpide_cf7_business_check = function ( $result, $tag ) use ( $wpide_cf7_free_providers, $wpide_cf7_business_fields ) {
if ( $wpide_cf7_business_fields && ! in_array( $tag->name, $wpide_cf7_business_fields, true ) ) {
return $result;
}
$email = isset( $_POST[ $tag->name ] ) ? strtolower( sanitize_email( wp_unslash( $_POST[ $tag->name ] ) ) ) : '';
$domain = (string) substr( strrchr( $email, '@' ), 1 );
if ( $domain && in_array( $domain, $wpide_cf7_free_providers, true ) ) {
$result->invalidate( $tag, 'Please use your work email address.' );
}
return $result;
};
add_filter( 'wpcf7_validate_email', $wpide_cf7_business_check, 20, 2 );
add_filter( 'wpcf7_validate_email*', $wpide_cf7_business_check, 20, 2 );
Notes
Limit it to certain fields by adding their names to the list at the top; leave the list empty to check every email field.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.