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.

Another optimization is to ask WordPress to run the init of sitemap as early as possible. The function “add_action” takes a third parameter which is called “priority”.

Setting this to zero (default value ids 10) WordPress calls our initialization function as soon as possible and before other plugins with higher priority value.

This is good because if our plugin has to do something (to generate the sitemap), after that no other will happen (it call “php die”) so it’s unuseful to let other plugins to (slowly) initialized when they are no more used.

On my blogs, change the priority makes big (really big) difference.

After those changes, a new version is available: [download id=”10″]

One reply on “Tutorial: Write a Sitemap Plugin Day 7”

I’m a beginner, trying to set up a website where visitors have the option of leaving their e-mail so they can be notified when I make a new post. That would allow me to start building a database for later use, and possibly capture traffic numbers. I thought this plug-in might be the answer, but your tutorials are obviously written for those knowledgeable about websites, so, not knowing the terminology, I can’t follow the instructions.

Leave a Reply