Last active
December 24, 2015 10:29
-
-
Save sc0ttkclark/6784248 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function enhanceins_zip_code_check( $result, $value, $form, $field ) { | |
global $wpdb; | |
if ( !$result[ 'is_valid' ] ) { | |
return $result; | |
} | |
if ( !is_array( $value ) ) { | |
$value = explode( ',', $value ); | |
} | |
foreach ( $value as $val ) { | |
$zipcode = ''; | |
if ( isset( $_POST[ 'zipcode_' . $val ] ) ) { | |
$zipcode = $_POST[ 'zipcode_' . $val ]; | |
} | |
//make sure we have something to work with | |
if ( empty( $zipcode ) ) { | |
$result[ 'is_valid' ] = false; | |
$result[ 'message' ] = 'Please provide a zip code that you want.'; | |
} | |
//make sure it's a valid value as well. | |
elseif ( strlen( $zipcode ) > 5 || ! is_numeric( $zipcode ) ) { | |
$result[ 'is_valid' ] = false; | |
$result[ 'message' ] = 'Please provide a valid zip code.'; | |
} | |
//if we're good so far, check in the database | |
else { | |
$sql = "SELECT zip FROM " . $wpdb->prefix . "zipcodes WHERE zip = %s LIMIT 1"; | |
$exists = $wpdb->get_var( $wpdb->prepare( $sql, $zipcode ) ); | |
if ( !empty( $exists ) ) { | |
$result[ 'is_valid' ] = false; | |
$result[ 'message' ] = 'Please provide an existing zip code'; | |
} | |
} | |
} | |
return $result; | |
} | |
add_filter( 'gform_field_validation_3_1', 'enhanceins_zip_code_check', 10, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment