Created
August 18, 2013 21:14
-
-
Save namwen/6264048 to your computer and use it in GitHub Desktop.
Used this in a store locator implementation. When the custom post type was saved, I grabbed the meta info ( address ) and used it to get the lat & long. through Google's geocode API.
This file contains 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
add_action('save_post','my_update_shop'); | |
/* add_action('post_updated','my_update_shop'); */ | |
function my_update_shop($post_id){ | |
// Autosaves call this function but mess up the data saved to the DB, so we ignore them | |
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){ | |
return $post_id; | |
} | |
// Only go through if we're using the shop post type | |
if ( $_POST['post_type'] == 'shop'){ | |
// Set up the data from the meta boxes | |
$address = $_POST['_cmb_shop_address']; | |
$city = $_POST['_cmb_shop_city']; | |
$state = $_POST['_cmb_shop_state']; | |
$zip = $_POST['_cmb_shop_zip']; | |
$phone = $_POST['_cmb_shop_phone']; | |
$website = $_POST['_cmb_shop_site']; | |
$title = get_the_title($post_id); | |
// Build the string from the address details entered | |
$requestString = $address.', '.$city.', '.$state.', '.$zip; | |
// If the custom field is empty, the store is new and needs to be inserted | |
if( empty($_POST['_cmb_shop_lat']) ){ | |
// Make the request and put it in an array called coordinates | |
$coordinates = makeGeocodeRequest($requestString); | |
$lat = $coordinates['latitude']; | |
$lng = $coordinates['longitude']; | |
insertStoreLocatorTable($post_id, $title, $lat, $lng, $requestString, $phone, $website); | |
}else{ | |
// If it's not empty we should update it in case the address has changed | |
// Make the request and put it in an array called coordinates | |
$coordinates = makeGeocodeRequest($requestString); | |
$lat = $coordinates['latitude']; | |
$lng = $coordinates['longitude']; | |
updateStoreLocatorTable($post_id, $title, $lat, $lng, $requestString, $phone, $website); | |
} | |
// Save the lat and lng as post meta | |
$_POST['_cmb_shop_lat'] = $lat; | |
$_POST['_cmb_shop_lng'] = $lng; | |
} | |
else{ | |
return; | |
} | |
} | |
/* Makes the curl call to google and returns the lat and lng in an array */ | |
function makeGeocodeRequest($string){ | |
$string = str_replace(" ", "+", $string); | |
$requestURL = "http://maps.googleapis.com/maps/api/geocode/json?address=".$string."&sensor=false"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $requestURL); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$response = json_decode(curl_exec($ch), true); | |
if($response['status'] != 'OK'){ | |
return null; | |
} | |
// Get the data from the response | |
$geometry = $response['results'][0]['geometry']; | |
$latitude = $geometry['location']['lat']; | |
$longitude = $geometry['location']['lng']; | |
// Build the array to return | |
$array = array( | |
'latitude' => $latitude, | |
'longitude' => $longitude | |
); | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment