Skip to content

Instantly share code, notes, and snippets.

@tomdevisser
Last active March 31, 2025 14:23
Show Gist options
  • Save tomdevisser/164853382ae695f68d51822673f4ae7c to your computer and use it in GitHub Desktop.
Save tomdevisser/164853382ae695f68d51822673f4ae7c to your computer and use it in GitHub Desktop.
<?php
// Add your Google Maps API key, preferrably in wp-config.php or in the database.
define( 'GOOGLE_MAPS_API_KEY', '' );
function acf_google_map_api( $api ) {
$api['key'] = GOOGLE_MAPS_API_KEY;
return $api;
}
add_filter( 'acf/fields/google_map/api', 'acf_google_map_api' );
function fetch_and_save_google_map_data( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) ) return;
if ( ! isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$city = '';
$address = '';
// Checking the POST request for the address and city, change these field keys.
if ( ! empty( $_POST['acf']['field_67ea988ce2fbb'] ) ) {
$city = $_POST['acf']['field_67ea988ce2fbb'];
}
if ( ! empty( $_POST['acf']['field_67ea9878e2fba'] ) ) {
$address = $_POST['acf']['field_67ea9878e2fba'];
}
if ( $city && $address ) {
$full_address = urlencode( $address . ', ' . $city . ', Netherlands' ); // Hardcoded The Netherlands, could be a field as well.
$api_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $full_address . '&key=' . GOOGLE_MAPS_API_KEY;
$response = wp_remote_get( $api_url );
if ( is_wp_error( $response ) ) return;
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );
if ( empty( $data->results ) ) return;
$result = $data->results[0];
$location = $result->geometry->location;
$latitude = $location->lat;
$longitude = $location->lng;
$components = $result->address_components;
$address_data = array(
'street_number' => '',
'street_name' => '',
'city' => '',
'state' => '',
'state_short' => '',
'post_code' => '',
'country' => '',
'country_short' => ''
);
foreach ( $components as $component ) {
if ( in_array( 'street_number', $component->types ) ) {
$address_data['street_number'] = $component->long_name;
}
if ( in_array( 'route', $component->types ) ) {
$address_data['street_name'] = $component->long_name;
}
if ( in_array('locality', $component->types ) ) {
$address_data['city'] = $component->long_name;
}
if ( in_array('administrative_area_level_1', $component->types ) ) {
$address_data['state'] = $component->long_name;
$address_data['state_short'] = $component->short_name;
}
if ( in_array('postal_code', $component->types ) ) {
$address_data['post_code'] = $component->long_name;
}
if ( in_array('country', $component->types ) ) {
$address_data['country'] = $component->long_name;
$address_data['country_short'] = $component->short_name;
}
}
$google_map_data = array(
'address' => $result->formatted_address,
'lat' => $latitude,
'lng' => $longitude,
'zoom' => 14, // This is the default zoom level.
'place_id' => $result->place_id ?? '',
'name' => $address_data['street_name'] . ' ' . $address_data['street_number'],
'street_number' => $address_data['street_number'],
'street_name' => $address_data['street_name'],
'city' => $address_data['city'],
'state' => $address_data['state'],
'state_short' => $address_data['state_short'],
'post_code' => $address_data['post_code'],
'country' => $address_data['country'],
'country_short' => $address_data['country_short']
);
update_field( 'location', $google_map_data, $post_id ); // Finally, update the 'location' field name if needed.
}
}
add_action( 'save_post', 'fetch_and_save_google_map_data' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment