Created
July 3, 2025 09:16
-
-
Save saifsultanc/1f0ecc43801d54940920ab24d68f8ac3 to your computer and use it in GitHub Desktop.
gpaa-geoencode-addresses.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'init', function() { | |
| $form_id = 401; // Replace with your Form ID. | |
| $field_id = 1; // Replace with your Address Field ID. | |
| $api_key = 'YOUR_GOOGLE_API_KEY'; // Replace with your Google Maps API Key. | |
| if ( ! class_exists( 'GFAPI' ) ) { | |
| return; | |
| } | |
| $search_criteria = [ | |
| 'status' => 'active', | |
| ]; | |
| $paging = [ | |
| 'offset' => 0, | |
| 'page_size' => 50, | |
| ]; | |
| do { | |
| $entries = GFAPI::get_entries( $form_id, $search_criteria, null, $paging ); | |
| foreach ( $entries as $entry ) { | |
| $entry_id = $entry['id']; | |
| // Meta keys | |
| $lat_key = "gpaa_lat_{$field_id}"; | |
| $lng_key = "gpaa_lng_{$field_id}"; | |
| $place_key = "gpaa_place_id_{$field_id}"; | |
| $raw_key = "gpaa_raw_value_{$field_id}"; | |
| // Check existing meta | |
| $lat = gform_get_meta( $entry_id, $lat_key ); | |
| $lng = gform_get_meta( $entry_id, $lng_key ); | |
| if ( empty( $lat ) || empty( $lng ) ) { | |
| $address = rgar( $entry, $field_id ); | |
| if ( empty( $address ) ) { | |
| continue; | |
| } | |
| $coords = get_address_geocode( $address, $api_key ); | |
| if ( $coords ) { | |
| gform_update_meta( $entry_id, $lat_key, $coords['lat'] ); | |
| gform_update_meta( $entry_id, $lng_key, $coords['lng'] ); | |
| gform_update_meta( $entry_id, $place_key, $coords['place_id'] ); | |
| gform_update_meta( $entry_id, $raw_key, $coords['raw_value'] ); | |
| error_log( "✅ Updated entry #{$entry_id} with coords for address: {$address}" ); | |
| } else { | |
| error_log( "⚠️ Failed to geocode entry #{$entry_id} for address: {$address}" ); | |
| } | |
| } | |
| } | |
| $paging['offset'] += $paging['page_size']; | |
| } while ( count( $entries ) >= $paging['page_size'] ); | |
| } ); | |
| function get_address_geocode( $address, $api_key ) { | |
| if ( empty( $address ) || empty( $api_key ) ) { | |
| return false; | |
| } | |
| $address_encoded = urlencode( $address ); | |
| $url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address_encoded}&key={$api_key}"; | |
| $response = wp_remote_get( $url ); | |
| if ( is_wp_error( $response ) ) { | |
| return false; | |
| } | |
| $body = wp_remote_retrieve_body( $response ); | |
| $data = json_decode( $body, true ); | |
| if ( ! empty( $data['results'][0] ) ) { | |
| $result = $data['results'][0]; | |
| return [ | |
| 'lat' => $result['geometry']['location']['lat'], | |
| 'lng' => $result['geometry']['location']['lng'], | |
| 'place_id' => $result['place_id'], | |
| 'raw_value' => $result['formatted_address'], | |
| ]; | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment