• Resolved Alnatroz75018

    (@alnatroz75018)


    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;
    }

    https://www.ads-software.com/plugins/contact-form-7/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    It’s close, but this

    if('postcode' == $type){

    always results false because the $type is “text*” in the case.

    The $name is “postcode” (a name is the second word in a form-tag, a type is the first one) so change the line to:

    if('postcode' == $name){

    Also, you shouldn’t edit plugin files. You can customize in another place like your theme’s functions.php.

    Thread Starter Alnatroz75018

    (@alnatroz75018)

    Dear Takayuki,
    thank you for looking into my issue.

    I have done the modification (this time in the plugin editor in WP)

    Unfortunatly it does not seem to work. whatever I populate in the postcode field, the circular arrows keep turning and nothing happens

    the modified code is the following

    add_filter('wpcf7_validate_text','postcode_custom_filter', 10, 2); // postcode field
    add_filter('wpcf7_validate_text*', 'postcode_custom_filter', 10, 2); // Req. postcode 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' == $name){ // Only apply to fields with the form field name of "postcode"
    		$the_value = $_POST[$name];
    		if( postcode_in_area($the_value)){ // Is not in our area
    			$result['valid'] = false;
    			$result['reason'][$name] = "sorry we do not operate in this area";
    		}
    	}
    	return $result;
    }
    Thread Starter Alnatroz75018

    (@alnatroz75018)

    Hi again,

    I believe the error comes from “preg_match”, as it is probably not the best way to check the exact value of the postcode. Is there a way to have a list of postcodes and match the result entered in the form against these values?

    Thank you in advance

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Use in_array.

    Thread Starter Alnatroz75018

    (@alnatroz75018)

    thank you Takayuki, I used the following code if this can help someone and it works

    add_filter('wpcf7_validate_text','postcode_custom_filter', 10, 2); // postcode field
    add_filter('wpcf7_validate_text*', 'postcode_custom_filter', 10, 2); // Req. postcode field
    // Add custom validation for CF7 form fields on postcode
    
    function postcode_in_area($postcode){ // check if the postcode is in our area
    	$areaArray = array('75001', '75002', '75003', '75004');
    
        if (!in_array($postcode, $areaArray)) {
                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' == $name){ // Only apply to fields with the form field name of "postcode"
    		$the_value = $_POST[$name];
    		if( postcode_in_area($the_value)){ // Is not in our area
    			$result['valid'] = false;
    			$result['reason'][$name] = "Sorry, we do not operate in this area.";
    		}
    	}
    	return $result;
    }

    Alnotorz –
    I’m trying to incorporate the same function into my website but I’m having a hard time to get the coding to work. Can you walk me through what you did to get the coding to work?

    I’ve add your code Alnatroz.
    But he will not check my submitted postcode.

    Do you have any suggestion?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘filter on the postcode (ZIP code)’ is closed to new replies.