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)";
}else{
     $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')";
}

Leave a Reply