Integrating Google XML Sitemaps with Your WordPress Plugin

Sunday, 12th July 2009

Note: Please read my more recent post about Integrating Google XML Sitemaps with the latest version of WP e-Commerce using a plugin by Lee Willis.

One plugin I recommend that everyone should add to their WordPress installation is the Google XML Sitemaps plugin.

This plugin will automatically generate an XML sitemap of your posts and pages and ping supporting search engines to notify them that the sitemap has been updated.

But the best thing about this plugin for other plugin developers is it provides hooks so you can ‘borrow’ it’s functionality so that your plugin can add additional pages into the XML sitemap that is generated.

So if you’re a WordPress plugin developer, read on…

How to add pages to the XML sitemap

To illustrate how to implement the XML Sitemaps hook, I will base the example on the WP e-Commerce plugin (please note, I originally implemeted this with version 3.6.12 of the WP e-Commerce plugin so it may not work with more recent versions). We will create a function that will add all of this plugin’s product pages into the XML sitemap produced by Google XML Sitemaps.

The Google XML Sitemaps plugin provides the hook “sm_buildmap” which gets triggered whenever the plugin re-builds the sitemap file. We are going to create a function “wp_ecommerce_sm” which will get called by this hook.

So, here’s the function code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function wp_ecommerce_sm() {
   global $wpdb;
   if ( class_exists('GoogleSitemapGenerator') ) {
      $generatorObject = &GoogleSitemapGenerator::GetInstance();
      $sql = "SELECT DISTINCT * FROM `" . $wpdb->prefix . "product_list` WHERE `active` IN('1') ORDER BY `id` DESC";
      $product_list = $wpdb->get_results($sql, ARRAY_A);
      foreach ( $product_list as $product ) {
         $purchase_link = wpsc_product_url($product['id']);
         $purchase_link = str_replace('&', '&', $purchase_link);
         if ($generatorObject != null) $generatorObject->AddUrl($purchase_link, time(), "daily", 0.5);
      }
   }
}
add_action("sm_buildmap", "wp_ecommerce_sm");

The main points to note in this function are…

Check that the Google XML Sitemaps exists (is installed):

3
if ( class_exists('GoogleSitemapGenerator') ) {

Get an instance of the Google XML Sitemap plugin:

4
$generatorObject = &GoogleSitemapGenerator::GetInstance();

Loop through all our additional pages that we want to add to the sitemap, in this case the WP e-Commerce product pages:

5
6
7
8
9
$sql = "SELECT DISTINCT * FROM `" . $wpdb->prefix . "product_list` WHERE `active` IN('1') ORDER BY `id` DESC";
$product_list = $wpdb->get_results($sql, ARRAY_A);
foreach ( $product_list as $product ) {
   // Add pages to sitemap here
}

And within our loop, add our pages like this:

10
if ($generatorObject != null) $generatorObject->AddUrl($purchase_link, time(), "daily", 0.5);

And lastly we need to add the hook to call this function whenever the XML sitemap is rebuilt:

add_action("sm_buildmap", "wp_ecommerce_sm");

Is that all there is to it?

Well, not really. Your still have to trigger the sitemap to re-build to include your new pages. You can do this manually by going to your XML Sitemaps setting page and clicking the link to rebuild to force the sitemap to re-build. Ideally however you would try to get your plugin to do this automatically whenever you add/edit/delete one of your pages. I couldn’t find an obvious hook to easily do this so that is something I would like to see adding in a future version of the Google XML Sitemaps plugin.

Download

Download the above code packaged as a WordPress plugin using the link below:

Click here to download...

new york interior designers
raspberry ketone free trial

9 Responses to “Integrating Google XML Sitemaps with Your WordPress Plugin”

  1. charismeki Says:

    hi ben,

    thx for the work but it does not work in my sitemap. where to paste the functions code in the sitemap-generator.php exactly or doesn’t it matters? I rebuild the sitemap manually and cleared the cache but no products in the sitemap.xml. would be nice to get help. i am running 3.6.12wp-ec and 3.1.4sg-xml.

    thanks and best regards,
    markus

  2. Ben Says:

    I have added a link above to download the code packaged as a WordPress plugin. Install and activate this plugin, then re-build your siyemap manually using the Google XML Sitemap plugin, then check to see if your products have been added to the sitemap.xml

  3. charismeki Says:

    woah but gives me this fatal error when trying to activate: Fatal error: Cannot redeclare wp_ecommerce_sm() (previously declared in /srv/www/vhosts/eisenbams.de/httpdocs/wp-content/plugins/sitemap-generator/sitemap-generator.php:504) in /srv/www/vhosts/eisenbams.de/httpdocs/wp-content/plugins/wp-e-commerce-sm.php on line 19

  4. Ben Says:

    You will need to remove the wp_ecommerce_sm function that you previously tried to add to the sitemaps plugin.

  5. charismeki Says:

    you were right. plugin is activated now, rebuild the sitemap but without any effect. products don’t show up in the sitemap.xml . hope not to bother you. best, m

  6. Ben Says:

    Is your site online that I could have a look at?

  7. charismeki Says:

    yes: eisenbams.de

  8. Andy Says:

    Hi, Any joy getting this working with 3.7.4? Plug in installed, but isn’t adding to the sitemap

  9. Peter Says:

    I had to make one minor change to the plugin

    Change the sql query to

    $sql = “SELECT DISTINCT * FROM `” . WPSC_TABLE_PRODUCT_LIST.”` WHERE `active` IN(’1′) ORDER BY `id` DESC”;

Leave a Reply