Tutorial: Write a Sitemap Plugin Day 8

Today I complete two easy tasks:

  • intercept the URL http://www.myblog.com/sitemap.xml to serve the sitemap (it will work only with WordPress on Apache and permalink active)
  • ping Google when something happens in WordPress (eg. a post changed)

The two modifications are very easy. The first is a matter to do a little parsing of request uri:

substr($_SERVER[‘REQUEST_URI’], strlen($_SERVER[‘REQUEST_URI’])-11) == ‘sitemap.xml’

and to add a checkbox in the options panel to enable this feature.


Tutorial: Write a Sitemap Plugin Day 7

If you try the sitemap plugin on a big blog, the sitemap generated can be broken. This is not an error of the plugin, but almost surely an out of memory error.

In PHP there are many way to try to save memory, in out plugin I will change the main loop in this way:

  • I copy the current post data in a variable called $post (an associative array)
  • I add the data (url, priority, change frequency) to this variable
  • I use the variable to create the URL sitemap entry
  • I unset the variable $posts

Printing the memory usage show there is no so big difference between the two methods, but is enough to make the plugin work on one of my sites.


Tutorial: Write a Sitemap Plugin Day 6

It’s time to make this sitemap plugin a little bit more “social”. How? As you learnt in previous lessons, WordPress has hooks that are called to give a way to “others” to make actions (over the default behaviour of WordPress).

There are a lot (really a lot) of these hooks… and now we add one for the sitemap plugin.

Any time a sitemap URL entry is generate by our plugin, our hook lets other plugins to do something related to that entry. An example is a plugin like Global Translator: for each post the plugin is processing, may be there is a translated version in one or more languages. Global Translator can attach to our hook and give us more entries to be added to the sitemap.


Tutorial: Write a Sitemap Plugin Day 5

Today I add an options panel to the Sitemap plugin. Before start coding I have to define what I want this panel to do and the data to collect.

  • the max number of urls to be added to the sitemap, blank value will mean “all”
  • a checkbox to enable the integration with other plugins (features I’ll explain on next lessons)

On the WordPress codex there are example on how to write an option panel. I don’t follow them, usually, seen that my panels are too much complex.


Tutorial: Write a Sitemap Plugin Day 4

We are at the end. At least the last day to have the plugin really working. After this lesson, I will start a new series to add some features to this plugin (control panel, options, integration with other plugins).

The last step is to print out XML elements that represent each single URL of our blog posts. On first lesson I showed up the XML format of those entries:



Tutorial: Write a Sitemap Plugin Day 2

Now we have a clear idea of the final product, so we can start to structure the plugin. Opsss, and the name? It can sound incredible but on the WordPress Plugin Directory the name “sitemap” is available.

So our plugin will be named “sitemap”.

Let’s start creating a folder named “sitemap” and the file “plugin.php” inside it. I’m used to call the main plugin file “plugin.php” it’s clear to understand (in a plugin there will be more than one php file). Surely, if you’re working with an IDE (like NetBeans) on more than one plugin, this name can create confusion (if you are a new by programmer…).


Tutorial: Write a Sitemap Plugin Day 1

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!