Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.