Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Geolocate on post save
- function dcom_geolocate_locations($post_id){
- global $post;
- if(get_post_type() == 'dcom_auto_locations'){
- $fields = false;
- // load from post
- if( isset($_POST['fields']) )
- {
- $fields = $_POST['fields'];
- //'<pre>'.var_dump($fields).'</pre>';
- $address = '';
- foreach($fields as $key=>$value){
- if($key == 'field_523c578bad0ed' || $key == 'field_523c57a5ad0ee'){
- $address .= $value.' ';
- }
- }
- }
- /* Request passes all checks; update the post's metadata */
- if(!empty($address)){
- $location = $address;
- $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
- $output = json_decode($geocode);
- if($output->status == 'OK'){
- $latitude = $output->results[0]->geometry->location->lat;
- $longitude = $output->results[0]->geometry->location->lng;
- if($latitude){
- update_post_meta($post->ID, 'dcom_auto_latitude', $latitude);
- }
- if($longitude){
- update_post_meta($post->ID, 'dcom_auto_longitude', $longitude);
- }
- }
- }
- }
- }
- add_action('save_post', 'dcom_geolocate_locations');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement