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.

To proceed in a clear way, I create an “options.php” which will be a file containing the HTML and logic of the control panel. Then I register a new menu void in WordPress to make this panel accessible. The call to register a new menu voice will be added to “plugin.php” and is:

add_action('admin_menu', 'sitemap_admin_menu');
function sitemap_admin_menu()
    add_options_page('Sitemap', 'Sitemap', 'manage_options', 'sitemap/options.php');

note that always I prefix function names with “sitemap_” as explained in the first lessons to avoid name conflicts.

The “options.php” file is too long to be inserted here, you can find it on the plugin package. Just a foundamental note: each options, at HTML level, is named “options[nnn]”. This convention produces an associative array when the options are extracted and are saved under a single name within the options table of WordPress. WordPress manage tha saving of an array, no problem. Tha option name will be “sitemap”.

To complete the lesson, I modify further “plugin.php” to manage the max number of url to insert. I do it directly on the query to save time and memory.

$query = "select ID as id, post_modified as lastmod from " . $wpdb->prefix .
"posts where post_type in ('post', 'page') and
post_status='publish' order by post_modified desc";

if ($options['max']) $query .= ' limit ' . $options['max'];

All lessons can be found under “sitemap tutorial

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

Leave a Reply