emrahgunduz
always eats his vegetables
Blog RSS Feed
  • twitter
  • friendfeed
  • linkedin
  • facebook
  • vimeo
  • flickr
  • lastfm

Create sitemap.xml In WordPress Without Using Any Plugins

Everybody likes plugins, and most of them are really great (in case of WordPress). But some people like me, tend to do the work by themselves.

So, here is a simple code that will re-create a sitemap.xml file, everytime you publish a post or page. Copy the code and paste it to your theme’s functions.php file (wp-content/themes/your_theme_folder/functions.php).

PS. I added <?php … ?> for clean code coloring, you will not need those lines.

<?php add_action("publish_post", "eg_create_sitemap"); add_action("publish_page", "eg_create_sitemap"); function eg_create_sitemap() {   $postsForSitemap = get_posts(array(     'numberposts' => -1,     'orderby' => 'modified',     'post_type'  => array('post','page'),     'order'    => 'DESC'   ));     $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';   $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';     foreach($postsForSitemap as $post) {     setup_postdata($post);         $postdate = explode(" ", $post->post_modified);         $sitemap .= '<url>'.       '<loc>'. get_permalink($post->ID) .'</loc>'.       '<lastmod>'. $postdate[0] .'</lastmod>'.       '<changefreq>monthly</changefreq>'.     '</url>';   }     $sitemap .= '</urlset>';     $fp = fopen(ABSPATH . "sitemap.xml", 'w');   fwrite($fp, $sitemap);   fclose($fp); } ?>

If you’ve got custom post types, you’ll need to add the registered taxonomy to ‘post_type’ array part of the code.

For a big blog with thousands of posts, creating a sitemap.xml file with every post publish action might not be a good solution, so keep that in mind.

This post's short url is: http://emrg.me/5v

11 responses for Create sitemap.xml In WordPress Without Using Any Plugins

  1. me says:

    Nice tutorial, but where the result for sitemap.xml i must place it. Thank you…

    • emrahgunduz says:

      It has to be in the root folder of your domain (in most cases it is the /www or the /public_html folder of your host)

  2. rakesh kumar says:

    Nice and clean code , Do you know how we can zip it.

  3. Ntx says:

    thanks, its work for my twenty ten child theme… excellent!!

  4. Dan O'Neil says:

    Thanks for this – another plugin bites the dust!

    Is there a simple way to exclude certain pages? I have them listed in my robots.txt file, but don’t really want protected files and download info pages listed in the sitemap!

  5. nomadone says:

    Wow neat thanks for sharing.

    I’m having a problem with sitemap plugins due to the size of my site, it’s 50 000+ posts, will this script allow for creating such a large sitemap?

    • emrahgunduz says:

      The script creates a sitemap.xml by looping through all the posts in the site. So this script is not a good solution.

      If you are familiar enough with PHP, you can change the script to prepend the new post to the sitemap.xml, not construct from zero.

      First you’ll need to change the get_posts part of the script. Follow this link to the WordPress codex for get_post function. All you’ll need is to get the last post or page published.
      You can then prepend string to files with something like this.

    • emrahgunduz says:

      … and always, before testing anything, backup your sitemap.xml :)

  6. Saif Bechan says:

    Working great! I don’t like to use plugins for tasks that are so simple. This is an excellent solution!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <span style=""> <p> <li> <ol>

Pings and trackbacks

  1. [...] Untuk membuat sitemap tanpa plugin ini kita cukup menambah beberapa kode fungsi  di file functions.php theme wordPress yang anda gunakan. Tanpa panjang lebar silahkan kunjungi alamat url berikut ini tips mebuat sitemap tanpa plugin di sini [...]

Calendar

May 2012
M T W T F S S
« Feb    
 123456
78910111213
14151617181920
21222324252627
28293031  
Web Analytics
Author: Emrah Gunduz