Wordpress GeoTag Hack

This is a little technical post for all of you geeks out there. I’ve really appreciated some of the features of the WordPress blog engine. One of the really unique features is the ability to attach latitude and longitude information to a particular post and for it to display a link to one of the various mapping sites out there. However, I also really appreciate the various blog posting clients (Azure, wBloggar, Zempt) for creating and editing my posts. But none of these clients has the geographic tagging ability built into it, which is especially important for mobile blogging. The following hack lets you enter latitude and longitude as XML tags within the post content and they’ll be stripped out and turned into the standard GeoTags.

There may be a more elegant solution to this problem, but I have to admit that I’m not much of a PHP programmer and am learning the syntax as I go. Additionally, I tried to research the ability to use the hack functionality in WordPress but couldn’t make heads or tails of it. So if you have any advice on how to make this cleaner, please let me know. In either case, you need to put this code into your xmlrpc.php file starting at line 63:

//Latitude **and** Longitude Hack.  Nathan Lorenz 2004
// Attempt to retreive Latitude **and** Logitude tags from Post Content.
$LatStart = strpos**(**$post\_content, "<lat"**)**;
$LonStart = strpos**(**$post\_content, "<lon"**)**;

//Decide which SQL Statement to Execute.
**if****(****(**$LatStart!==false**)** && **(**$LonStart!==false**)****)****{**
     //Retreive Latitude Information
     $LatEnd = strpos**(**$post\_content, ">", $LatStart**)**;
     $LatNumStart = strpos**(**$post\_content, """, $LatStart**)**;
     $LatNumEnd = strpos**(**$post\_content, """, $LatNumStart + 1**)**;
     $LatNum = **substr****(**$post\_content, $LatNumStart + 1, $LatNumEnd\-$LatNumStart\-1**)**;

     //Retreive Longitude Information
     $LonNumStart = strpos**(**$post\_content, """, $LonStart**)**;
     $LonNumEnd = strpos**(**$post\_content, """, $LonNumStart + 1**)**;
     $LonNum = **substr****(**$post\_content, $LonNumStart + 1, $LonNumEnd\-$LonNumStart\-1**)**;

     //Remove XML from Post
     //Remove Latitude
     $post\_content = **substr****(**$post\_content, 0, **(****int****)**$LatStart**)** . **substr****(**$post\_content, **(****int****)**$LatEnd + 1**)**;

     //Because the Postions in the string have changed, we need to again find the start **and** end of the Logitude tag.
     $LonStart = strpos**(**$post\_content, "<lon"**)**;
     $LonEnd = strpos**(**$post\_content, ">", $LonStart**)**;
     //Now we can remove the Longitude tag.
     $post\_content = **substr****(**$post\_content, 0, **(****int****)**$LonStart**)** . **substr****(**$post\_content, **(****int****)**$LonEnd + 1**)**;
     ///Convert **values** into float variables
     $post\_lat = floatval**(**$LatNum**)**;
     $post\_lon = floatval**(**$LonNum**)**;

     $sql = "INSERT INTO $tableposts
	(post\_author, post\_date, post\_content, post\_title, post\_excerpt, post\_category, post\_status, post\_lat, post\_lon)
	VALUES ('$post\_author','$post\_date','$post\_content','$post\_title',
	'$post\_excerpt','$post\_cat', '$post\_status', $post\_lat, $post\_lon)";
     $sql = "INSERT INTO $tableposts
	(post\_author, post\_date, post\_content, post\_title, post\_excerpt, post\_category, post\_status)
	VALUES ('$post\_author','$post\_date','$post\_content','$post\_title', '$post\_excerpt','$post\_cat', '$post\_status')";

See also