Support » Plugins and Hacks » WP Geo » Post Location via Email like this [New York, NY] or {37.961523,-120.413452}

Post Location via Email like this [New York, NY] or {37.961523,-120.413452}

  • 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.


    Just through this into a php file and save it in your plugin directory!

    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');


Viewing 1 replies (of 1 total)
  • Plugin Author Ben Huson


    Is this functionality that you would like to release on the WP repository as a separate add-on plugin?

    I like the functionality.

    I think it would be nice if you could use curly brackets for both scenarios (so that people don’t confuse with shortcodes). You could simply check if the content of the curly brackets is 2 comma separate numbers then treat it as lat/lng, otherwise treat it as an address.

Viewing 1 replies (of 1 total)
  • The topic ‘Post Location via Email like this [New York, NY] or {37.961523,-120.413452}’ is closed to new replies.
Skip to toolbar