Here are some new thoughts on the Wordpress geocaching plugin. I think the initial project needs to work as follows.
- Create the php function(s) that will read and write the necessary .gpx files. Initially, I want a simple php page that displays the information from a .gpx file. Utimately, however, the information for each geocache will actually be stored in the database. The resultant page will need to create the .gpx file on-the-fly.
- It would also be helpful if an existing .gpx file could be uploaded, and its content added directly to the database.
- A custom page type needs to be produced, which will display the necessary .gpx information within Wordpress.
- The custom page type will not be part of a theme. It needs to be created by a plugin. So this plugin needs to be able to create database table(s) to store the necessary information, and create the custom page type.
Here are some of the issues that I found.
Initially I thought the simplexml_load_file() function could be used. However, there is a snag. Some of the tags in .gpx files contain colons. For example:
<groundspeak:owner id=”344279″>pftaylor61</groundspeak:owner>
<groundspeak:type>Unknown Cache</groundspeak:type>
<groundspeak:container>Small</groundspeak:container>
<groundspeak:difficulty>2.5</groundspeak:difficulty>
I got round this by simply reading the entire file into a string variable, then using the str_replace() function to replace the colons with underscores. So a <groundspeak:type> tag would be turned into <groundspeak_type> This was achieved thus:
$filename = “testfile.gpx”;
$gpxfile = file_get_contents($filename) or die(“Error: Cannot create object”);
$gpxfile = str_replace(“groundspeak:”,”groundspeak_”,$gpxfile);
$gpx = simplexml_load_string($gpxfile);
Now the xml elements can be read using nodes. For example, the long description was accessed as follows:
<?php
echo $gpx->wpt->groundspeak_cache->groundspeak_long_description;
?>
Any thoughts on this, and the other suggestions, would be very welcome.