Skip to content

Instantly share code, notes, and snippets.

@sc0ttkclark
Last active December 24, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sc0ttkclark/6784248 to your computer and use it in GitHub Desktop.
Save sc0ttkclark/6784248 to your computer and use it in GitHub Desktop.
<?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