Skip to content

Instantly share code, notes, and snippets.

@verticalgrain
Last active September 4, 2017 17:55
Show Gist options
  • Save verticalgrain/7f377bbf857766f76193 to your computer and use it in GitHub Desktop.
Save verticalgrain/7f377bbf857766f76193 to your computer and use it in GitHub Desktop.
Wordpress filter - Geocode postal code custom field to latitude and longitude custom fields - Gravity forms, regular custom fields
<?php
/**
* Geocodes a postal code field from a gravity form, saves latitude and longitude to custom fields
* To use this, change $entry[26] to the id of your gravity form field that contains the postal code - i.e. $entry[2]
* To use this, change the name of the geo_latitude and geo_longitude custom fields to what you've created.
* For bonus points, name your lat and long custom fields geo_latitude and geo_longitude, to be in line with Wordpress standard Geodata: https://codex.wordpress.org/Geodata
*
*
*/
// Geocode an address on saving a gravity form
add_action('gform_after_submission', 'geocode_address_gravity_forms', 10, 2);
function geocode_address_gravity_forms($entry,$form) {
if(isset($entry[26]) && !empty($entry[26]))
{
// Form the url to geocode the postal code
$resp = wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($entry[26])."&sensor=false" );
if( is_wp_error( $resp ) ) {
// If there is an error reaching googleapis, populate the custom field with an error messageom field
$geocode_error = 'Error geocoding address';
update_post_meta($entry['post_id'], 'geo_latitude', $geocode_error);
update_post_meta($entry['post_id'], 'geo_longitude', $geocode_error);
} elseif ( 200 == $resp['response']['code'] ) {
$body = $resp['body'];
$data = json_decode($body);
if($data->status=="OK"){
// If the geocoding was successful, parse the json to get the lat and long
$latitude = $data->results[0]->geometry->location->lat;
$longitude = $data->results[0]->geometry->location->lng;
// Set the post custom fields
update_post_meta($entry['post_id'], 'geo_latitude', $latitude);
update_post_meta($entry['post_id'], 'geo_longitude', $longitude);
}
}
}
}
//
/*
* Another example that will geocode a postal code custom field and save the latitude and longitude as custom fields
* To use this, change $custom_fields["postal_code"] to the id of your gravity form field that contains the postal code - i.e. $custom_fields["my_postal_code"]
* To use this, change the name of the geo_latitude and geo_longitude custom fields to what you've created.
* For bonus points, name your lat and long custom fields geo_latitude and geo_longitude, to be in line with Wordpress standard Geodata: https://codex.wordpress.org/Geodata
*
*
*/
//add_action('save_post', 'geocode_address', 10, 2);
function geocode_address($post_id) {
$custom_fields = get_post_meta($post_id);
if(isset($custom_fields["postal_code"]) && !empty($custom_fields["postal_code"][0]))
{
$resp = wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($custom_fields["postal_code"][0])."&sensor=false" );
if( is_wp_error( $resp ) ) {
// If there is an error reaching googleapis, populate the custom field with an error messageom field
$geocode_error = 'Error geocoding address';
update_post_meta($post_id, 'geo_latitude', $geocode_error);
update_post_meta($post_id, 'geo_longitude', $geocode_error);
} elseif ( 200 == $resp['response']['code'] ) {
$body = $resp['body'];
$data = json_decode($body);
if($data->status=="OK"){
// If the geocoding was successful, parse the json to get the lat and long
$latitude = $data->results[0]->geometry->location->lat;
$longitude = $data->results[0]->geometry->location->lng;
// Set the post custom fields
update_post_meta($post_id, 'geo_latitude', $latitude);
update_post_meta($post_id, 'geo_longitude', $longitude);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment