KML (& NetworkLink) for Nearby & any Geo-WordPress!

Following the addition of GeoRSS, earlier today while out photographing some PhotoSpots (TBA), I had an even better (possibly) idea, now that my WordPress stores Lat/Long, why not output KML too! The icing on the cake is you can also wrap it in a NetworkLink so that peoples Google Earth will auto update. (I am surprised couldn’t find this done already!)

And if that isn’t enough then I also release the code under GPL :) – note its the first time written anything for WordPress, so might not be a tidy as could be!

Anyway see it in action on my series of PhotoSpots:

- as all these list the PhotoSpot category, each post includes a link to open the actual spot, so just click the world icon in the balloon to open :)

Download the Source or view the source: WordPress KML Generator and NetworkLink Wrapper

Installation: This is not a proper plugin, so just drop the .php files into the root of your WordPress folder and you should be done (but you can configure the icon to display if you wish by editing the wp-kml.php file)

Use: Normally you would only provide a link to the Wrapper – which in turn links to the feed itself, but the feed is useful like in the above example to plot directly on Google Maps. You can also specify a particular category to display like the above example or otherwise will include all geocoded posts.
Important: Requires the GeoPress extension be installed (to get geocode info)
Only tested on WordPress 2.0.1 – Works For Me – Your Mileage May Vary – no guarantee, implied or otherwise

15 Responses to “KML (& NetworkLink) for Nearby & any Geo-WordPress!”

  1. Aaron Spence says:

    G’day Barry,

    I love what you’ve done with this. I grabbed the php and put it into my site on the link below, but no go I’m afraid. Every post I do is geocoded with Geopress. My feeds are diverted to feedburner, which could be causing the problem. Any idea on how to modify the code accordingly? (I’m not a programmer )



  2. Barry says:

    HI Aaron,

    I’m not certain but I think its how you downloaded the files, did you view the page then click ‘view source’ in your browser (or right click the link and do ‘save target as’) – either way that won’t work (as you actully are saving color and syntex highlighted code.)

    Just click the links as normal and copy/paste the code from the browser window into your favorite text editor, hopefully that should work.
    I’ll put up a zip file to make it even easier…

  3. Aaron Spence says:

    I guess I just showed how much of a programmer I am not ;) I did as you suggested and it works perfectly. Extremely well done.

    If you copy and past that link into google maps now, it works beautifully.

    So my next sniveling & ungrateful question is…. how would I automatically produce a (simple?) map like this that contains all my posts, not just the latest RSS feed items?

    Did I mention that this was extemely cool…. hmmm don’t think so, I’ll mention it again. That is brilliant.

    Thanks, Aaron.

  4. Aaron Spence says:

    Thought I better make a direct link to the results of your great work for anyone else reading.

    That link will open up google maps page automatically generated by my blogs RSS feed.

    Thanks, Aaron.

  5. Barry says:

    hmm, after some digging I think putting this just _before_ the wp(‘feed=rss2′); line in wp-kml.php, should allow you to specify wp-kml-link.php?bhall=1 to get all.

    if (isset($_GET['bhall'])) {

    note, this is extremely hacky, not fully tested – I don’t have enough posts – but might work, use at own risk! (I won’t put it in the default download, as this would be dangerous on blogs with many posts)

  6. Aaron Spence says:

    Thanks Barry,

    I gave it a run, first altering just the wp-kml.php file, then doing the same to the wp-kml-link.php file, but no go I’m afraid. (you may want to check my files, ’cause as you’ve seen I ain’t the programmer)

    I note the dire warnings about using this type of thing above, I guess as the post numbers rise, things will eventually break. So…. how about being able to populate the google map by tag. ie I’m doing Hobart at the moment in my blog, once I’m finished with Hobart, I’d love to have a google map with all the hobart posts on it. (Created automatically with your code of course :)

    Don’t know if it is possible, but I’m happy to pay to make it happen :)

    Thanks, Aaron.

  7. Barry says:

    > how about being able to populate the google map by tag

    You can do that now with the categories (which are ecentially tags in wp?), .php?cat=20 or .php?category_name=photospot ,

    but will still run into problems with the map only displaying as many points as you have items in your rss feed (I guess an option in WP changes this) – if the above fix didnt work then will need more study of the WP documentation – I’ll take a look if get a chance…

  8. Hi Barry – very neat addition. If you don’t mind, I am going to bundle these additions into GeoPress (with credit & license of course), and they will show up in the next release.

    Regarding Aaron’s request for the # of items. It appears as though WordPress force overrides the $posts_per_page when it is a feed, so if you just do wp(); instead of specifying the feed, then you get all the items.

    As you point out, this is hacky, because you lose any other logic that WordPress uses for outputting feed-formatted information. A little more digging may reveal being able to specify an action to override the number of posts – this could then be incorporated into GeoPress as a setting: “Number of locations in KML feed”


  9. Aaron Spence says:

    G’day Barry,

    I’ve been playing around with my blog and your code and have it working as pointed out in your last comment. ie I can specify a category (tag) for the output, and it will output all the posts I have in that category up to the number of items I have specified in my RSS feed. (In this case 20)

    This is fine for the time being as any categories I want to use will have less than 20 posts.

    I’m now reworking my post formatting etc to have it work better in google maps. That way people will be able to browse my blog on an area by using google maps which is very cool.

    Thanks again, and I’ll post a link as soon as I’m done with the redesign.


  10. Aaron Spence says:

    G’day Barry,

    I’ve finally got all my posts updated to the new format to work better inside google maps. I’ve also created a ‘Previous Destinations’ page to allow viewers to check out regional blog posts in google maps.

    I’m extremely pleased with how it’s all working, and once again thank you for your great work.


  11. Barry says:

    Thanks Andrew,

    yes if you can use the code please do! It’s only a quick adaptation of the wp-rss2.php file so there might be a better way to package it, also I guess really would be nice to add a rewrite rule for it.

    As for the number of items, yes it would be nice to have a setting, from the quick investigation earlier didn’t spot an easy way because it essentially using the rss feed functions just outputting kml.

    Aaron – glad to see the code been put to good use!

  12. Aaron Spence says:

    G’day Barry,

    Sorry to hassel you again. Tried out my google mini-mashup this morning, and it’s not working, when yesterday it was fine. AFAIK I haven’t changed anything, but you never know.

    I came over to check your mini-mashup to see if it was a google issue & yours is gone. Although it’s not showing the error all mine are ie ‘contains errors’.

    So my question, is your mini-mashup still working (and I’ve messed up my system somehow) or has google changed something?

    Thanks, Aaron.

  13. Aaron Spence says:


    I does appear the problem is on my end. When I attempt to load the following page into my brower:

    it is stopped at line 91 which would execute the following line from the wp-kml.php file, but appears reticent to do it.

    I know this isn’t your problem, but any light you could shed on this to help me track down the problem would be much appreciated.

    Thanks, Aaron.

  14. Aaron Spence says:

    Update Continued:

    I note the code I pasted has been stripped out. It is line 96 in the wp-kml.php file.

    -metadata–? print convert_chars etc

    Thanks, Aaron.

  15. Aaron Spence says:

    Update – Fixed.

    Andrew Turner one of the Geopress devs didn’t know what was causing the problem, but had updated the code a little. He sent me the updated code, and it’s all working fine once again, just as before.

    Rather crazy that code can work one day and not the next, but there you have it, the wonderful world of programming, of which I am thankfully, not a part :)

    Thanks again, Aaron.