Skip to content

Instantly share code, notes, and snippets.

@hereswhatidid
Created March 13, 2014 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hereswhatidid/9537156 to your computer and use it in GitHub Desktop.
Save hereswhatidid/9537156 to your computer and use it in GitHub Desktop.
Geo code on save
<?php
//Auctions should be geocoded on save
function geocode_auction($post_id){
$auction_slug = 'auctions';
$_POST += array("{$auction_slug}_edit_nonce" => '');
if ( $auction_slug == $_POST['post_type'] && current_user_can( 'edit_post', $post_id )) {
$address = get_post_meta(get_the_ID(), 'Location', true);
if($address != ""){
$ourFileName = "http://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&sensor=false";
$str_data = file_get_contents($ourFileName);
$data = json_decode($str_data,true);
foreach ($data as $location) {
$newLat = $location[0].geometry.location.lat();
$newLng = $location[0].geometry.location.lng();
}
if($newLat){
update_post_meta($post_id, 'auction_lat', $newLat);
}
if($newLng){
update_post_meta($post_id, 'auction_long', $newLng);
}
}
}
}
add_action('save_post','geocode_auction');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment