{"id":136,"date":"2011-11-11T10:41:28","date_gmt":"2011-11-11T16:41:28","guid":{"rendered":"http:\/\/kurtzenisek.com\/articles\/?p=136"},"modified":"2016-01-11T09:23:41","modified_gmt":"2016-01-11T15:23:41","slug":"contact-form-7-add-a-custom-verificationvalidation-filter","status":"publish","type":"post","link":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/","title":{"rendered":"Contact Form 7: Add a custom verification\/validation filter"},"content":{"rendered":"<p>The following PHP code is fully working when placed in the theme&#8217;s functions.php file or in a plugin. This checks the email address provided against a list of free email providers to better ensure that the only submissions you receive are using a company email address. It&#8217;s isolated to only be applied to email fields with the form field name of &#8220;company-email&#8221;.<\/p>\n<p>That said, this can be easily modified to do whatever validation needs to be done. Just change out the function that returns true\/false based on a condition, change the form field name it&#8217;s looking for, swap out the fail message, and change the form field that it&#8217;s applied to.<\/p>\n<p><code>\/\/ Add custom validation for CF7 form fields<br \/>\nfunction is_company_email($email){ \/\/ Check against list of common public email providers &amp; return true if the email provided doesn't match one of them<br \/>\nif(<br \/>\npreg_match('\/@gmail.com\/i', $email) ||<br \/>\npreg_match('\/@hotmail.com\/i', $email) ||<br \/>\npreg_match('\/@live.com\/i', $email) ||<br \/>\npreg_match('\/@msn.com\/i', $email) ||<br \/>\npreg_match('\/@aol.com\/i', $email) ||<br \/>\npreg_match('\/@yahoo.com\/i', $email) ||<br \/>\npreg_match('\/@inbox.com\/i', $email) ||<br \/>\npreg_match('\/@gmx.com\/i', $email) ||<br \/>\npreg_match('\/@me.com\/i', $email)<br \/>\n){<br \/>\nreturn false; \/\/ It's a publicly available email address<br \/>\n}else{<br \/>\nreturn true; \/\/ It's probably a company email address<br \/>\n}<br \/>\n}<br \/>\nfunction custom_email_validation_filter($result,$tag){<br \/>\n$type = $tag['type'];<br \/>\n$name = $tag['name'];<br \/>\nif($name == 'company-email'){ \/\/ Only apply to fields with the form field name of \"comany-email\"<br \/>\n$the_value = $_POST[$name];<br \/>\nif(!is_company_email($the_value)){ \/\/ Isn't a company email address (it matched the list of free email providers)<br \/>\n$result['valid'] = false;<br \/>\n$result['reason'][$name] = 'You need to provide an email address that isn\\'t hosted by a free provider.<br \/>\nPlease contact us directly if this isn\\'t possible.';<br \/>\n}<br \/>\n}<br \/>\nreturn $result;<br \/>\n}<br \/>\nadd_filter('wpcf7_validate_email','custom_email_validation_filter', 10, 2); \/\/ Email field<br \/>\nadd_filter('wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2); \/\/ Req. Email field<\/code><\/p>\n<p>Also, I hope the developer of Contact Form 7 knows how great he is for making this sort of thing possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following PHP code is fully working when placed in the theme&#8217;s functions.php file or in a plugin. This checks the email address provided against a list of free email providers to better ensure that the only submissions you receive are using a company email address. It&#8217;s isolated to only be applied to email fields [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"episode_type":"","audio_file":"","podmotor_file_id":"","podmotor_episode_id":"","cover_image":"","cover_image_id":"","duration":"","filesize":"","filesize_raw":"","date_recorded":"","explicit":"","block":"","itunes_episode_number":"","itunes_title":"","itunes_season_number":"","itunes_episode_type":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-136","post","type-post","status-publish","format-standard","hentry","category-news"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Contact Form 7: Add a custom verification\/validation filter - Kurt Zenisek<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Contact Form 7: Add a custom verification\/validation filter - Kurt Zenisek\" \/>\n<meta property=\"og:description\" content=\"The following PHP code is fully working when placed in the theme&#8217;s functions.php file or in a plugin. This checks the email address provided against a list of free email providers to better ensure that the only submissions you receive are using a company email address. It&#8217;s isolated to only be applied to email fields [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/\" \/>\n<meta property=\"og:site_name\" content=\"Kurt Zenisek\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/kzeni\" \/>\n<meta property=\"article:published_time\" content=\"2011-11-11T16:41:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-01-11T15:23:41+00:00\" \/>\n<meta name=\"author\" content=\"Kurt\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kurt\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/contact-form-7-add-a-custom-verificationvalidation-filter\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/contact-form-7-add-a-custom-verificationvalidation-filter\\\/\"},\"author\":{\"name\":\"Kurt\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#\\\/schema\\\/person\\\/f3528f54a8466c58edb0533fcb1c2ec9\"},\"headline\":\"Contact Form 7: Add a custom verification\\\/validation filter\",\"datePublished\":\"2011-11-11T16:41:28+00:00\",\"dateModified\":\"2016-01-11T15:23:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/contact-form-7-add-a-custom-verificationvalidation-filter\\\/\"},\"wordCount\":145,\"articleSection\":[\"News\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/contact-form-7-add-a-custom-verificationvalidation-filter\\\/\",\"url\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/contact-form-7-add-a-custom-verificationvalidation-filter\\\/\",\"name\":\"Contact Form 7: Add a custom verification\\\/validation filter - Kurt Zenisek\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#website\"},\"datePublished\":\"2011-11-11T16:41:28+00:00\",\"dateModified\":\"2016-01-11T15:23:41+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#\\\/schema\\\/person\\\/f3528f54a8466c58edb0533fcb1c2ec9\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/contact-form-7-add-a-custom-verificationvalidation-filter\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/contact-form-7-add-a-custom-verificationvalidation-filter\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/contact-form-7-add-a-custom-verificationvalidation-filter\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Contact Form 7: Add a custom verification\\\/validation filter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/\",\"name\":\"Kurt Zenisek\",\"description\":\"Assorted Code Snippets &amp; Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/#\\\/schema\\\/person\\\/f3528f54a8466c58edb0533fcb1c2ec9\",\"name\":\"Kurt\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g\",\"caption\":\"Kurt\"},\"sameAs\":[\"http:\\\/\\\/kzeni.com\",\"https:\\\/\\\/www.facebook.com\\\/kzeni\",\"https:\\\/\\\/x.com\\\/KZeni\"],\"url\":\"https:\\\/\\\/kurtzenisek.com\\\/articles\\\/author\\\/kzeni\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Contact Form 7: Add a custom verification\/validation filter - Kurt Zenisek","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/","og_locale":"en_US","og_type":"article","og_title":"Contact Form 7: Add a custom verification\/validation filter - Kurt Zenisek","og_description":"The following PHP code is fully working when placed in the theme&#8217;s functions.php file or in a plugin. This checks the email address provided against a list of free email providers to better ensure that the only submissions you receive are using a company email address. It&#8217;s isolated to only be applied to email fields [&hellip;]","og_url":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/","og_site_name":"Kurt Zenisek","article_author":"https:\/\/www.facebook.com\/kzeni","article_published_time":"2011-11-11T16:41:28+00:00","article_modified_time":"2016-01-11T15:23:41+00:00","author":"Kurt","twitter_misc":{"Written by":"Kurt","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/#article","isPartOf":{"@id":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/"},"author":{"name":"Kurt","@id":"https:\/\/kurtzenisek.com\/articles\/#\/schema\/person\/f3528f54a8466c58edb0533fcb1c2ec9"},"headline":"Contact Form 7: Add a custom verification\/validation filter","datePublished":"2011-11-11T16:41:28+00:00","dateModified":"2016-01-11T15:23:41+00:00","mainEntityOfPage":{"@id":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/"},"wordCount":145,"articleSection":["News"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/","url":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/","name":"Contact Form 7: Add a custom verification\/validation filter - Kurt Zenisek","isPartOf":{"@id":"https:\/\/kurtzenisek.com\/articles\/#website"},"datePublished":"2011-11-11T16:41:28+00:00","dateModified":"2016-01-11T15:23:41+00:00","author":{"@id":"https:\/\/kurtzenisek.com\/articles\/#\/schema\/person\/f3528f54a8466c58edb0533fcb1c2ec9"},"breadcrumb":{"@id":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kurtzenisek.com\/articles\/contact-form-7-add-a-custom-verificationvalidation-filter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kurtzenisek.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Contact Form 7: Add a custom verification\/validation filter"}]},{"@type":"WebSite","@id":"https:\/\/kurtzenisek.com\/articles\/#website","url":"https:\/\/kurtzenisek.com\/articles\/","name":"Kurt Zenisek","description":"Assorted Code Snippets &amp; Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kurtzenisek.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kurtzenisek.com\/articles\/#\/schema\/person\/f3528f54a8466c58edb0533fcb1c2ec9","name":"Kurt","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d243c95df571db785649246aa036b5c16c30e9dc0dbcf2fc24297b1a5d6eebd8?s=96&d=mm&r=g","caption":"Kurt"},"sameAs":["http:\/\/kzeni.com","https:\/\/www.facebook.com\/kzeni","https:\/\/x.com\/KZeni"],"url":"https:\/\/kurtzenisek.com\/articles\/author\/kzeni\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/p2Xgc6-2c","_links":{"self":[{"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/posts\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/comments?post=136"}],"version-history":[{"count":4,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/posts\/136\/revisions"}],"predecessor-version":[{"id":187,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/posts\/136\/revisions\/187"}],"wp:attachment":[{"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/media?parent=136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/categories?post=136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kurtzenisek.com\/articles\/wp-json\/wp\/v2\/tags?post=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}