Skip to content

Instantly share code, notes, and snippets.

@cyberhobo
Last active August 29, 2015 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberhobo/576aa79a9f1d246d7817 to your computer and use it in GitHub Desktop.
Save cyberhobo/576aa79a9f1d246d7817 to your computer and use it in GitHub Desktop.
WordPress hook to geocode Gravity Forms fields for Geo Mashup
<?php
/**
* Example WordPress hook to geocode Gravity Forms fields for Geo Mashup.
*
* This example uses a form ID of 2, replace _2 with your form ID,
* 'address' and 'zip' with your Gravity Forms field names, and prefix
* with your unique namespace prefix.
*/
add_action( 'gform_after_submission_2' 'prefix_gform_after_submission_2', 10, 2 );
function prefix_gform_after_submission_2( $lead, $form ) {
// Require a post id
if ( empty( $lead['post_id'] ) )
return;
// Require Geo Mashup
if ( !class_exists( 'GeoMashupDB' ) )
return;
// Geocode address fields
$postmeta_address_fields = array(
'address',
'zip',
);
$geocode_address_parts = array();
foreach( $postmeta_address_fields as $postmeta_address_field ) {
$field_value = get_post_meta( $lead['post_id'], $postmeta_address_field, true );
if ( !empty( $field_value ) ) {
// Remove commas to avoid mixing them up with ours
$geocode_address_parts[] = str_replace( ',', ' ', $field_value );
}
}
if ( empty( $geocode_address_parts ) )
return;
$location = GeoMashupDB::blank_location();
if ( GeoMashupDB::geocode( implode( ', ', $geocode_address_parts ), $location ) )
GeoMashupDB::set_object_location( 'post', $lead['post_id'], $location );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment