I wrote a number of plugins and every time I review the code of them I found someting can be improved or adjusted. I’m not talking about bugs but different way to implement the plugin actions. This tutorial will be a set of post where I dissect “my way to write a plugin”. I don’t think it’s the best, most complete tutorial, but it m ay be useful… so… lets start.
Before all we need to decide the plugin purpose, it’s features, it’s limits. I want to write a very (very) simple plugin to generate a very (very) simple XML sitemap of this blog. As you know there are such kind of plugins, there is no need to write a new one, but this is a tutorial!
Here the aims:
- generate a sitemap in XML formt as specified on www.sitemap.org (compatible with Google Sitemap)
- generate the sitemap on the fly, calling a specific URL on my blog, like www.satollo.net/?sitemap=xml
- I don’t care about register virtual URL for the sitemap with WordPress (it’s too hard)
- the sitemap will contains only the home page, the single posts and the single pages (no tag pages, no archive pages): I need search engine to surf my articles…
The sitemap protocol requires to generate things like the one blow:
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.example.com/</loc> <lastmod>2005-01-01</lastmod> <changefreq>monthly</changefreq> <priority>0.8</priority> </url> </urlset>
As you see it’s a very simple XML. What do we need to extract from WordPress database?
- the home page URL
- single posts and pages URL (also called permalink)
- the posts and pages last change date
- eventually compute a priority
- choose a change frequency (eg, home page changes daily, a post ususally never changes)
I want to give the blog owner a configuration panel. Really there is almost nothing to configure… but we can use it to deliver some infos, put in it a donation link (ih ih ih) and… it’s too early to decide now.
What about the development IDE? A simple plugin like this one can be easly written with Notepad. I prefer to use NetBeans for PHP (I’m a Java analyst and use it for my daily job). NetBeans for PHP can be downloaded here, it’s only 26 megabyte. You need Java too, it’s enough to install the Java Runtime 1.6 before to install NetBeans (Sun changes links every other day… I hope this one will work).
All lessons can be found under “sitemap tutorial“