Integrating Google XML Sitemaps with Your WordPress Plugin
Sunday, 12th July 2009One 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...
July 27th, 2009 at 7:19 pm
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
July 28th, 2009 at 7:45 am
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
July 28th, 2009 at 8:23 am
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
July 28th, 2009 at 11:11 am
You will need to remove the wp_ecommerce_sm function that you previously tried to add to the sitemaps plugin.
July 28th, 2009 at 3:26 pm
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
July 28th, 2009 at 8:05 pm
Is your site online that I could have a look at?
July 29th, 2009 at 1:41 am
yes: eisenbams.de
September 2nd, 2009 at 10:09 pm
Hi, Any joy getting this working with 3.7.4? Plug in installed, but isn’t adding to the sitemap
October 11th, 2009 at 2:50 pm
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”;