Bricks Forms: Require a Business Email Address
Rejects Bricks form submissions that use free email providers such as Gmail or Outlook, for B2B quote and demo request forms.
Opens WPIDE on
Code
// Free email domains to refuse, and the error shown.
$wpide_bricks_free_domains = array( 'gmail.com', 'googlemail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'live.com', 'aol.com', 'icloud.com', 'gmx.com', 'proton.me' );
$wpide_bricks_free_message = 'Please use your work email address.';
add_filter( 'bricks/form/validate', function ( $errors, $form ) use ( $wpide_bricks_free_domains, $wpide_bricks_free_message ) {
if ( ! is_object( $form ) || ! method_exists( $form, 'get_fields' ) ) {
return $errors;
}
foreach ( (array) $form->get_fields() as $key => $value ) {
if ( 0 !== strpos( (string) $key, 'form-field-' ) || ! is_string( $value ) || ! is_email( $value ) ) {
continue;
}
$domain = strtolower( substr( strrchr( $value, '@' ), 1 ) );
if ( in_array( $domain, $wpide_bricks_free_domains, true ) ) {
$errors[] = $wpide_bricks_free_message;
break;
}
}
return $errors;
}, 10, 2 );
Notes
Checks every email field of every Bricks form. Edit the domain list to suit your audience.
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.