I have some data that I’m pulling from an XML source and would like to display it in WordPress.
One solution would be to take the static markup that my theme generates and spaghetti-code my page with some PHP that grabs the data from the XML and prints it in the page. Easy enough, but now every time I make a change to the theme I have to re-apply it to this page.
Is there a more elegant solution that would allow me to inherit the whole site look n’ feel and just append my data to it?
Thanks
I would make a plugin out of the code that you are using to pull the XML. I’ve done this before for a client that was using a calendaring system that was a set of PHP files. I wrapped the calls to the calendar files in a plugin, and just called what I needed to from there. This preserved the look and feel of the site without extra work there, and used WordPress’s native capabilities.
It’s fairly easy to take an existing set of code and wrap it as a plugin, and then you can use the methods of that plugin in your theme files. You could for example make a page to house the data, make a page-specific theme file that outputs (or not) the content of that page, and also use the theme file to run the methods for pulling in the XML data via your plugin.
Resources for writing (or wrapping code into) a plugin:
I would suggest writing shortcodes to manipulate your data. You can write a function that dumps your entire output with a neat little code placed in a post or you can go very in depth with shortcodes and give parameters to process or return data differently conditionally.
If it is straightforward and simple enough you can just place it inside your functions.php file. If you start to go in depth with it I would suggest you create a plugin to contain your code instead.
With a shortcode you can create a post that can process your xml and you can insert the data from the elements as so
[fromxml element=”xml_element”] or [fromxml]xml_element[/fromxml] or [fromxml source_file=”myfile.xml” element=”xml_element”] and so on…