Skip to content

Instantly share code, notes, and snippets.

@anthonybudd
Created March 9, 2017 11:04
Show Gist options
  • Save anthonybudd/b451eea818646d77d783689f5f9a36d6 to your computer and use it in GitHub Desktop.
Save anthonybudd/b451eea818646d77d783689f5f9a36d6 to your computer and use it in GitHub Desktop.
<?php
class City extends WP_Model
{
public $postType = 'city';
public $attributes = [
'latitude',
'longitude'
];
public function saving($model){
$output = json_decode(file_get_contents("http://maps.google.com/maps/api/geocode/json?address=". $model->title ."&sensor=false"));
$latitude = @$output->results[0]->geometry->location->lat;
$longitude = @$output->results[0]->geometry->location->lng;
if(!empty($latitude) && !empty($longitude)){
$model->latitude = $latitude;
$model->longitude = $longitude;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment