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.
















Nice tutorial, but where the result for sitemap.xml i must place it. Thank you…
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)
Nice and clean code , Do you know how we can zip it.
You can use zlip http://php.net/manual/en/book.zlib.php.
Zlib can directly compress a string and write it to the file. Also you can use gzip libraries written for php, just google for it.
But if the file is not too big, compressing it won’t make much of a difference.
thanks, its work for my twenty ten child theme… excellent!!
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!
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?
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.
… and always, before testing anything, backup your sitemap.xml
Working great! I don’t like to use plugins for tasks that are so simple. This is an excellent solution!