Skip to content

Instantly share code, notes, and snippets.

@cosbgn
Created February 28, 2017 21:50
Show Gist options
  • Save cosbgn/cbe9243ba6b482ff6c7fcfcc891f7cc6 to your computer and use it in GitHub Desktop.
Save cosbgn/cbe9243ba6b482ff6c7fcfcc891f7cc6 to your computer and use it in GitHub Desktop.
/* Enter Your Custom Functions Here */
// Add custom validation for CF7 form fields
function is_company_email($email){ // Check against list of common public email providers & return true if the email provided *doesn't* match one of them
if(
preg_match('/@gmail./i', $email) ||
preg_match('/@hotmail./i', $email) ||
preg_match('/@live./i', $email) ||
preg_match('/@msn./i', $email) ||
preg_match('/@aol./i', $email) ||
preg_match('/@yahoo./i', $email) ||
preg_match('/@inbox./i', $email) ||
preg_match('/@outlook./i', $email) ||
preg_match('/@gmx./i', $email) ||
preg_match('/@mac./i', $email) ||
preg_match('/@icloud./i', $email) ||
preg_match('/@mail./i', $email) ||
preg_match('/@zoho./i', $email) ||
preg_match('/@yandex./i', $email) ||
preg_match('/@me./i', $email)
){
return false; // It's a publicly available email address
}else{
return true; // It's probably a company email address
}
}
function custom_email_validation_filter($result, $tag) {
$tag = new WPCF7_Shortcode( $tag );
if ( 'company-email' == $tag->name ) {
$the_value = isset( $_POST['company-email'] ) ? trim( $_POST['company-email'] ) : '';
if(!is_company_email($the_value)){
$result->invalidate( $tag, "Please Enter a valid Business Email ID" );
}
}
return $result;
}
add_filter( 'wpcf7_validate_email', 'custom_email_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment