Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Last active September 30, 2025 00:01
Show Gist options
  • Save zackkatz/db258481779c5eb96f1ef3ef9064386f to your computer and use it in GitHub Desktop.
Save zackkatz/db258481779c5eb96f1ef3ef9064386f to your computer and use it in GitHub Desktop.
GravityView: Update ACF Google Maps fields on Edit Entry
<?php
/**
* Update ACF Google Maps field when editing entries via GravityView
*
* This snippet complements the GP Address Autocomplete ACF snippet:
* https://gravitywiz.com/snippet-library/gpaa-acf-google-maps/
*
* PROBLEM: The original GP snippet only runs when Advanced Post Creation initially
* creates a post. When you edit an entry through GravityView's Edit Entry feature,
* the ACF Google Maps field becomes empty because the snippet doesn't run again.
*
* SOLUTION: This code hooks into GravityView's entry update process and re-populates
* the ACF Google Maps field with the updated address data and coordinates.
*
* Requirements:
* - GravityView (with Edit Entry enabled)
* - GP Address Autocomplete
* - Advanced Custom Fields (ACF)
* - Gravity Forms Advanced Post Creation Add-on
*
* Installation:
* Add this code to your theme's functions.php or a custom plugin.
*
* @author GravityKit
* @link https://gravitykit.com/support
*/
add_action( 'gravityview/edit_entry/after_update', 'gv_update_acf_google_maps_on_edit', 10, 3 );
/**
* Update ACF Google Maps fields when entry is updated via GravityView
*
* @param array $form The Gravity Forms form object.
* @param int $entry_id The entry ID being updated.
* @param int $view_id The GravityView View ID.
*
* @return void
*/
function gv_update_acf_google_maps_on_edit( $form, $entry_id, $view_id ) {
// Validate all required dependencies
if ( ! is_callable( 'acf_get_field' ) ||
! is_callable( 'gp_address_autocomplete' ) ||
! function_exists( 'get_field_objects' ) ) {
return;
}
// Get the updated entry
$entry = GFAPI::get_entry( $entry_id );
if ( is_wp_error( $entry ) ) {
return;
}
// Get the post ID
$post_id = rgar( $entry, 'post_id' );
if ( ! $post_id ) {
return;
}
// Get Address Autocomplete fields from form
$autocomplete_fields = gp_address_autocomplete()->get_autocomplete_fields( $form );
if ( empty( $autocomplete_fields ) ) {
return;
}
// Get all ACF fields for this post
$acf_fields = get_field_objects( $post_id );
if ( ! $acf_fields ) {
return;
}
// Filter to only Google Map fields
$google_map_fields = array_filter( $acf_fields, function( $field ) {
return isset( $field['type'] ) && $field['type'] === 'google_map';
} );
if ( empty( $google_map_fields ) ) {
return;
}
// Update each Address Autocomplete field → ACF Google Map field
foreach ( $autocomplete_fields as $gf_field ) {
$gf_field_id = $gf_field->id;
// Get coordinates from entry
$lat = rgar( $entry, "gpaa_lat_{$gf_field_id}" );
$lng = rgar( $entry, "gpaa_lng_{$gf_field_id}" );
// Validate coordinates (0,0 is valid, so check for numeric and not empty string)
if ( ! is_numeric( $lat ) || ! is_numeric( $lng ) || $lat === '' || $lng === '' ) {
continue;
}
// Build formatted address from Address field components
$street = rgar( $entry, "{$gf_field_id}.1" );
$street2 = rgar( $entry, "{$gf_field_id}.2" );
$city = rgar( $entry, "{$gf_field_id}.3" );
$state = rgar( $entry, "{$gf_field_id}.4" );
$zip = rgar( $entry, "{$gf_field_id}.5" );
$country = rgar( $entry, "{$gf_field_id}.6" );
$address_parts = array_filter( [ $street, $street2, $city, $state, $zip, $country ] );
$address = implode( ', ', $address_parts );
if ( empty( $address ) ) {
continue;
}
// Prepare ACF Google Map value with proper type casting
$acf_value = [
'address' => $address,
'lat' => (float) $lat,
'lng' => (float) $lng,
];
// Update all ACF Google Map fields with this address
// (Usually there's only one, but this handles multiple map fields if needed)
foreach ( $google_map_fields as $acf_field ) {
update_field( $acf_field['key'], $acf_value, $post_id );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment