Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.