Putting the geo position in a Wordpress RSS feed

The last few days I’ve been looking at adding geographic information to articles on this website. For WordPress, the software this site is running on, I found the Geo plugin with which you can store geographic coordinates for an article. The plugin adds this information to the page of each article, but not in the RSS feed, so I made a function that does this, though not automatically.

To use it, start with adding the following function to the Geo plugin. I’m using version 1.0, don’t know if it works in earlier versions:

function get_the_rss_geotags()
/**
* Returns the tags with geo information for inclusion in RSS 2.0 feeds.
*
* Include a call to this function inside the <item> tag to
* add tags with geo information if available.
* To keep the feed valid add these namespace declarations to the <rss> tag:
*    xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
*    xmlns:icbm="http://postneo.com/icbm"
*    xmlns:geourl="http://geourl.org/rss/module/"
*/
{
	global $wp_query;

	if(!get_settings('use_geo_positions')) return;

	list($lat, $lon) = split(',', get_post_meta($wp_query->post->ID, '_geo_location', true));
	if ($lat == '' || $lon == '') {
		if (get_settings('use_default_geourl')){
			// send the default here
			$lat = get_settings('default_geourl_lat');
			$lon = get_settings('default_geourl_lon');
		}
	}
	if ($lat != '' && $lon != '') {
		echo "<geo :lat>$lat</geo><geo :long>$lon</geo>\n"
				. "<icbm :latitude>$lat</icbm><icbm :longitude>$lon</icbm>\n"
				. "<geourl :longitude>$lon</geourl><geourl :latitude>$lat</geourl>\n";
	}
}

Now you can add this line to wp-rss2.php, somewhere inside the <item> tag:

<?php echo get_the_rss_geotags(); ?>

Also remember to add these namespace declarations to the rss tag:

<rss version=”2.0”
	xmlns:geo=”http://www.w3.org/2003/01/geo/wgs84_pos#”
	xmlns:icbm=”http://postneo.com/icbm”
	xmlns:geourl=”http://geourl.org/rss/module/”>

That’s it.

Published on 2 october 2005, 3 responses.

Responses

  • Michael-John Turner
    25 februari 2006, 20:32

    Great - thanks for the info! I see the latest version of Geo already includes get_the_rss_geotags(), but, natrually enough, I still needed to patch wp-rss2.php to include the geo and ICBM tags.

  • Rob Schlüter
    25 februari 2006, 22:24

    Nice to see that my function is now included in the current Geo plugin.

  • Sameer
    3 august 2006, 12:36

    This is great. Thanks a lot!!