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).

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="">';

  foreach($postsForSitemap as $post) {

    $postdate = explode(" ", $post->post_modified);

    $sitemap .= '<url>'.
      '<loc>'. get_permalink($post->ID) .'</loc>'.
      '<lastmod>'. $postdate[0] .'</lastmod>'.

  $sitemap .= '</urlset>';

  $fp = fopen(ABSPATH . "sitemap.xml", 'w');
  fwrite($fp, $sitemap);

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.

19 thoughts on “Create sitemap.xml In WordPress Without Using Any Plugins”

    1. 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)

  1. 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!

  2. 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?

    1. 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.

  3. Nice tip. Actually for big blogs you can create another php script and set it up to run as a cron job periodically like once every night or so.

  4. Hello,
    I put the code in functions.php of my theme, but nothing works
    if I go , no page,
    I see nothing, is it necessary to create the sitemap.xml file,
    if yes, that it is necessary to put inside?
    Thank you ,very important

  5. Hi, love the script! I was wondering if there was a way to make it so that it would carry out its objective when you click a button instead of having it run each time a post is created / updated? I ask because I modified the code to carry some custom fields and when I create a new post, then post it, those custom fields don’t show up in the sitemap until I do an update on that post, or post another post (basically run the script again.) Any thoughts?

Leave a Reply