Skip to content

Instantly share code, notes, and snippets.

@adamwalter
Last active June 28, 2018 18:19
Show Gist options
  • Save adamwalter/db818c93326ede529b14b975bec28bf7 to your computer and use it in GitHub Desktop.
Save adamwalter/db818c93326ede529b14b975bec28bf7 to your computer and use it in GitHub Desktop.
Geocode address and insert into ACF map field
<?php
/**
* WP-CLI EVAL FILE
*
* Command: wp eval-file filename.php {run}
*
* Usage: If the 'run' argument is passed, this script will
* process the RUN block. Otherwise it will process the DRY RUN.
*
* https://github.com/wp-cli/wp-cli/wiki/API
*/
$run = false;
/**
* Check for run argument
*/
if (!empty($args) && isset($args[0])) {
if ($args[0] === 'run') {
$run = true;
}
}
// function to geocode address, it will return false if unable to geocode address
function geocode($address){
// url encode the address
$address = urlencode($address);
// google map geocode api url
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key=API_KEY_HERE";
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
// response status will be 'OK', if able to geocode given address
if($resp['status']=='OK'){
// get the important data
$lati = isset($resp['results'][0]['geometry']['location']['lat']) ? $resp['results'][0]['geometry']['location']['lat'] : "";
$longi = isset($resp['results'][0]['geometry']['location']['lng']) ? $resp['results'][0]['geometry']['location']['lng'] : "";
$formatted_address = isset($resp['results'][0]['formatted_address']) ? $resp['results'][0]['formatted_address'] : "";
// verify if data is complete
if($lati && $longi && $formatted_address){
// put the data in the array
$data_arr = array();
array_push(
$data_arr,
$lati,
$longi,
$formatted_address
);
return $data_arr;
}else{
return false;
}
}
else{
return false;
}
}
/**
* Our process
*/
$query = new WP_Query(array(
'post_type' => 'vtl_location',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
));
if ($query->have_posts()):
while ($query->have_posts()): $query->the_post();
$map = [
'address' => '',
'lat' => '',
'lng' => ''
];
$address = get_post_meta(get_the_id(), 'location_info_street_address', true);
$city = get_post_meta(get_the_id(), 'location_info_city', true);
$state = get_post_meta(get_the_id(), 'location_info_state_prov', true);
$code = get_post_meta(get_the_id(), 'location_info_postal_code', true);
$country = get_post_meta(get_the_id(), 'location_info_country', true);
if (!empty($address)) {
$map['address'] .= $address;
}
if (!empty($city)) {
$map['address'] .= ', ' . $city;
}
if (!empty($state)) {
$map['address'] .= ', ' . $state;
}
if (!empty($code)) {
$map['address'] .= ' ' . $code;
}
if (!empty($country)) {
$map['address'] .= ', ' . $country;
}
$data_arr = geocode($map['address']);
if ($data_arr) {
$map['lat'] = $data_arr[0];
$map['lng'] = $data_arr[1];
$map['address'] = $data_arr[2];
}
// DRY RUN
if ($run === false && $data_arr):
WP_CLI::line(WP_CLI::colorize("%RDry Run!"));
WP_CLI::success("Latitude: " . $map['lat']);
WP_CLI::success("Longitude: " . $map['lng']);
WP_CLI::success("Formatted address: " . $map['address']);
// RUN
elseif ($data_arr):
update_post_meta(get_the_id(), 'USE_ACF_FIELD_KEY_NOT_THE_NAME', $map);
WP_CLI::success("Map address: " . $map['address']);
else:
WP_CLI::line(WP_CLI::colorize("%RGeocode failed for%n: " . $map['address']));
endif;
endwhile;
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment