Tutorial: Write a Sitemap Plugin Day 7

Take a moment to subscribe my newsletter:

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: Sitemap 1.2.0 (172)

Do not forget to comment this lesson!
Insertion provided by Post Layout Pro of code on single post.
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!

Leave a Reply

Add an image