Created
March 13, 2014 21:16
-
-
Save hereswhatidid/9537156 to your computer and use it in GitHub Desktop.
Geo code on save
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
<?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