Just as the title says, this plugin will let you post a location from within your post's content.. great for emailing posts or using front-end posting plugins in combo.
REQUIRES CURL!
Just through this into a php file and save it in your plugin directory!
<?php
/*
Plugin Name: Geocode My Post
Description: Geocodes an address within a post's content. Include the address in [ ] ie. [San Francisco, CA]
*/
function geocode_my_post ($postID)
{
// Get post content.
$postContent = get_post_field('post_content', $postID);
// Search for location name.
$locationBegin = strpos($postContent, '[');
$locationEnd = strpos($postContent, ']');
if ( $locationBegin !== false && $locationEnd !== false && $locationEnd > $locationBegin )
{
// If found, geocode location.
$location = substr($postContent, ++$locationBegin, $locationEnd - $locationBegin);
// Make sure that the data is OK.
if ( preg_match('/^[A-Za-z0-9, ]+$/', $location) !== 1 ) { return; }
// Retrieve coordinates for this address from Google.
$connection = curl_init('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($location) . '&sensor=false');
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
$googleapiresponse = json_decode(curl_exec($connection), true);
// Do some error checking.
if ( is_array($googleapiresponse) && array_key_exists('status', $googleapiresponse) && $googleapiresponse['status'] == 'OK' )
{
$latitude = $googleapiresponse['results'][0]['geometry']['location']['lat'];
$longitude = $googleapiresponse['results'][0]['geometry']['location']['lng'];
// Save location coords to post meta.
add_post_meta($postID, '_wp_geo_latitude', $latitude, true) or update_post_meta($postID, '_wp_geo_latitude', $latitude);
add_post_meta($postID, '_wp_geo_longitude', $longitude, true) or update_post_meta($postID, '_wp_geo_longitude', $longitude);
}
}
//add lat lon if in {}
$locationgeoBegin = strpos($postContent, '{');
$locationgeoEnd = strpos($postContent, '}');
// If found, split lat/lon and add to meta.
if ( $locationgeoBegin !== false && $locationgeoEnd !== false && $locationgeoEnd > $locationgeoBegin )
{
$locationgeo = substr($postContent, ++$locationgeoBegin, $locationgeoEnd - 1);
list($latgeo,$longeo)=split(",", $locationgeo);
add_post_meta($post_id, '_wp_geo_latitude', $latgeo, true);
add_post_meta($post_id, '_wp_geo_longitude', $longeo, true);
}
}
// WAS 'publish_post' before WP 2.3
// add_action("{$new_status}_{$post->post_type}", 'geocode_my_post');
add_action("publish_post", 'geocode_my_post');
// add_action("trigger_mah_error", 'geocode_my_post');
?>