Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Save latitude and longitude to fields in Gravity Form submission using Geo My WP Input for Address
<?php
/**
* Save latitude and longitude to fields in Gravity Form submission using Geo My WP Plugin
*
* @author Joshua David Nelson, josh@joshuadnelson.com
*/
add_filter( 'gform_pre_submission', 'jdn_set_lat_long_values', 10, 5 );
function jdn_set_lat_long_values( $form ){
// find the location value
foreach( $form['fields'] as &$field ) {
if( $field['adminLabel'] == 'location' ){ // Find the field with an admin lable of 'location' - be sure to set this beforehand
$location = GEO_my_WP::geocoder( $_POST["input_{$field['id']}"], true ); // use the Geo My WP class' geocoder to get info
$latitude = $location['lat'];
$longitude = $location['lng'];
break;
}
}
if( !empty( $location ) && !empty( $latitude ) && !empty( $longitude ) ) {
// set the lat and long values
$i = 0;
foreach( $form['fields'] as &$field ) {
if( $field['adminLabel'] == 'latitude' ){
$_POST["input_{$field['id']}"] = $latitude;
$i++;
}
if( $field['adminLabel'] == 'longitude' ){
$_POST["input_{$field['id']}"] = $longitude;
$i++;
}
if( $i >=2 )
break;
}
} else { // Set alternative values, did this for error checking
$i = 0;
foreach( $form['fields'] as &$field ) {
if( $field['adminLabel'] == 'latitude' ){
$_POST["input_{$field['id']}"] = '25';
$i++;
}
if( $field['adminLabel'] == 'longitude' ){
$_POST["input_{$field['id']}"] = '32';
$i++;
}
if( $i >=2 )
break;
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.