Block Email Domains in Gravity Forms
Shows an error when someone enters an email address from a domain on your block list, such as throwaway inbox services, in any Gravity Forms email field.
Opens WPIDE on
Code
// Domains that may not be used in email fields.
$wpide_gf_blocked_domains = array( 'mailinator.com', 'yopmail.com', 'tempmail.com', 'guerrillamail.com', 'sharklasers.com' );
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) use ( $wpide_gf_blocked_domains ) {
if ( 'email' !== $field->type || ! $result['is_valid'] ) {
return $result;
}
$email = strtolower( trim( is_array( $value ) ? (string) reset( $value ) : (string) $value ) );
$domain = (string) substr( strrchr( $email, '@' ), 1 );
foreach ( $wpide_gf_blocked_domains as $blocked ) {
if ( $domain && ( $domain === $blocked || substr( $domain, -strlen( '.' . $blocked ) ) === '.' . $blocked ) ) {
$result['is_valid'] = false;
$result['message'] = 'Please use a different email address.';
break;
}
}
return $result;
}, 10, 4 );
Notes
Subdomains of listed domains are blocked as well; add more domains to the list as spam arrives.
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.