filter on the postcode (ZIP code)
-
Dear team,
I have tried hard to put a filter on a postcode in the CF7.The idea is to display a message when the postcode is not in the area we operate in.
I used the following code in the text.php file (as you can guess I am pretty new in PHP and only customed what I could find on the web)
This doesn’t work and drives me crazy. Any help would be appreciated.
The postcode is a text on the form and has the id:postcode
this line in the form:
<p>[text*postcode /4 id:postcode placeholder “postcode”]<p>The following code is in the text.php in C:\wamp\www\wordpress\wp-content\plugins\contact-form-7\modules
add_filter('wpcf7_validate_text','postcode_custom_filter', 10, 2); // Email field add_filter('wpcf7_validate_text*', 'postcode_custom_filter', 10, 2); // Req. Email field // Add custom validation for CF7 form fields on postcode function postcode_in_area($postcode){ // check if the postcode is in our area if( preg_match('2010', $postcode) || preg_match('2011', $postcode) || preg_match('2013', $postcode) || preg_match('2014', $postcode) || preg_match('2015', $postcode) || preg_match('2016', $postcode) || preg_match('2017', $postcode) || preg_match('2018', $postcode) || preg_match('2019', $postcode) ){ return true; // It's an area we operate in }else{ return false; // It's an area we do NOT service } } function postcode_custom_filter($result,$tag){ $type = $tag['type']; $name = $tag['name']; if('postcode' == $type){ // Only apply to fields with the form field name of "Your-Postcode" $the_value = $_POST[$name]; if( postcode_in_area($the_value)){ // Isn't a company email address (it matched the list of free email providers) $result['valid'] = false; $result['reason'][$name] = "sorry we do not operate in this area"; } } return $result; }
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘filter on the postcode (ZIP code)’ is closed to new replies.