Generating an XML Sitemap

For quite sometime, I used various plugins to create Sitemap for my blogs or other web sites that I had built on WordPress platform. However, lately I came across an amazing snippet by a fellow web developer. He wrote a very simple snippet that can create major search engine friendly XML Sitemap for any WordPress site. I also maintains the XML schema for the Sitemap protocol.

Let’s take a look at the snippet:

add_action('publish_post','do_generate_sitemap');
function do_generate_sitemap() {
   // post links
   $all_posts = get_posts(array(
      'numberposts' => -1,
      'orderby' => 'post_date',
      'post_type' => array('post','page','xxxx'),
      'post_status' => 'publish',
      'order' => 'DESC')
   );
   $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
   $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
   foreach ($all_posts as $post) {
      $sitemap .= '<url><loc>'.get_permalink($post->ID).'</loc></url>';
   }
   // category links
   $all_cats = get_terms(array('taxonomy' => 'category','hide_empty' => true));
   foreach ($all_cats as $scat) {
      $sitemap .= '<url><loc>'.get_term_link($scat).'</loc></url>';
   }
   // tag links
   $all_tags = get_terms(array('taxonomy' => 'post_tag','hide_empty' => true));
   foreach ($all_tags as $stag) {
      $sitemap .= '<url><loc>'.get_term_link($stag).'</loc></url>';
   }
   $sitemap .= '</urlset>';
   $path = fopen(ABSPATH.'sitemap.xml','w');
   fwrite($path, $sitemap);
   fclose($path);
}

This is pretty much the standard format of the snippet that you need to copy and paste on your theme’s functions.php file. However, I wouldn’t recommend you to use it directly.

Before You Use It: Don’t just copy and paste the above snippet. Little understanding is necessary to get the most out of your sitemap. First thing first, take a look at the line number seven. On post_type array I added “xxxx” to let users to add Custom Post Type or CPT on their sitemap. However, if you are not using CPT, you can simply us it like this:

'post_type' => array('post','page'),

I also added “Page” which I think is also important. In case if you don’t want you can delete it as well just like the way we deleted the CPT section.

Note: This is a bare-bone structure for a simple sitemap and I am not going crazy about validating and all. There are lot more that you can do with your own sitemap. However, this sitemap works and Google accepts happily without any hiccups. You can checkout how sitemap.org formatted their own sitemap before getting too complicated about it.

Update (Jul 15,2017): It turns out using this snippet (within functions.php file) before custom post type register function generates wrong (default URL rules) permalink on “sitemap.xml” file. However, using after the custom post register function resolves the function. Thanks.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.