If ZIP code in the list show one form. If no - show another [RESOLVED]

Hello! My client wants to receive a form which when entering ZIP postcode (which is in the list) displays the order form. If there is no ZIP in the list, the contact form displays.

Me finded that solution in the community here: Validate ZIP Code against a list of codes - #16 by evanfraser

/**
 * gform zip code validator.
 */
function gform_zip_checker($result, $value, $form, $field) {
	$file = get_stylesheet_directory_uri().'/zips.txt'; // I have this in my child theme

	if ( $result['is_valid'] && (!preg_match('/^' . $value . '$/m', file_get_contents($file))) ) {
		$result['is_valid'] = false;
		$result['message'] = 'We don\'t provide service to this area. If you believe this is incorrect please call us.';
	}
	return $result;
}
add_filter('gform_field_validation_2_8', 'gform_zip_checker', 10, 4); // gform_field_validation_FORMID_FIELDID

Should I create two another different forms and how I can call it using that example code?

Thank you, you’re the best.

Lana

My current idea is use gform_confirmation() but I have no idea about check the form field, not form id. My form ID is 1, and ZIP code field is 3. That code isn’t work:

add_filter( 'gform_confirmation_1_3', 'custom_confirmation', 10, 4 );
function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
    $file = get_stylesheet_directory_uri().'/zips.txt';

    if ( $result['is_valid'] && (!preg_match('/^' . $value . '$/m', file_get_contents($file))) ) {
        $confirmation = array( 'redirect' => 'https://www.google.com' );
    }else{
        $confirmation = array( 'redirect' => 'https://www.microsoft.com' );
    }

    return $confirmation;
}

Eventually I created Frankenstein but it works. :slight_smile:

function gform_zip_checker($result, $value, $form, $field) {
	$file = get_stylesheet_directory_uri().'/zips.txt'; // I have this in my child theme

	if ( $result['is_valid'] && (!preg_match('/^' . $value . '$/m', file_get_contents($file))) ) {
        add_filter( 'gform_confirmation_1', 'custom_confirmation', 10, 4 );
        function custom_confirmation( $confirmation, $form, $entry, $ajax ) {
            $confirmation = array( 'redirect' => 'https://www.google.com' );
            return $confirmation;
        }

	}else{
        add_filter( 'gform_confirmation_1', 'custom_confirmation1', 10, 4 );
        function custom_confirmation1( $confirmation, $form, $entry, $ajax ) {
            $confirmation = array( 'redirect' => 'https://www.microsoft.com' );
            return $confirmation;
        }        
    }
    return $result;

}
add_filter('gform_field_validation_1_3', 'gform_zip_checker', 10, 4); 
1 Like