Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I like to hardcode everything on my WordPress websites, without using any plugins. Is there any way to generate or update my sitemap everytime I publish/update a post on one of my Multisite blogs, without using plugins?

share|improve this question
You realize that there is no real difference between "hardcode" and a plugin besides the fact the plugins are portable and thus easier to work with? –  Wyck Jan 20 '12 at 18:53
Downvote for the "without a Plugin" restriction. –  Chip Bennett Feb 20 '12 at 17:47
Not wanting to add ANOTHER plugin on a CLIENT site is perfectly acceptable for such a simple task. It's true the code is the same but perhaps the OP didn't know that –  jasondavis May 23 '13 at 3:45
add comment

1 Answer

I dont know whether this works on multisite or not but it is working perfect for single wordpress installation for me. check it once may be useful for you.

when you create/update any posts or page it will generate sitemap.xml file and update the links (url's) in latest first order (Last modified.)

copy and paste the below code in your active themes functions.php file

    /* function to create sitemap.xml file in root directory of site  */

//add_action("publish_post", "eg_create_sitemap");
//add_action("publish_page", "eg_create_sitemap");

add_action("save_post", "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 .= "\n".'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";

  foreach($postsForSitemap as $post) {

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

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

  $sitemap .= '</urlset>';

  $fp = fopen(ABSPATH . "sitemap.xml", 'w');
  fwrite($fp, $sitemap);
share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.