WordPress.org

Ready to get started?Download WordPress

Forums

WP Geo
Post Location via Email like this [New York, NY] or {37.961523,-120.413452} (2 posts)

  1. allinfinite
    Member
    Posted 1 year ago #

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

    http://wordpress.org/extend/plugins/wp-geo/

  2. Ben Huson
    Member
    Plugin Author

    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic