Auto Publish sitemap.xml


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // Create sitemap.xml with each publish
  4. // Original http://emrahgunduz.com/category/categories/development/wordpress/
  5.  
  6. add_action("publish_post", "eg_create_sitemap");
  7. add_action("publish_page", "eg_create_sitemap");
  8.  
  9. function eg_create_sitemap() {
  10. $postsForSitemap = get_posts(array(
  11. 'numberposts' => -1,
  12. 'orderby' => 'modified',
  13. 'post_type' => array('post','page'),
  14. 'order' => 'DESC'
  15. ));
  16. $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
  17. $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  18.  
  19. foreach($postsForSitemap as $post) {
  20. setup_postdata($post);
  21.  
  22. $postdate = explode(" ", $post->post_modified);
  23.  
  24. $sitemap .= '<url>'.
  25. '<loc>'. get_permalink($post->ID) .'</loc>'.
  26. '<lastmod>'. $postdate[0] .'</lastmod>'.
  27. '<changefreq>monthly</changefreq>'.
  28. '</url>';
  29. }
  30.  
  31. $sitemap .= '</urlset>';
  32.  
  33. $fp = fopen(ABSPATH . "sitemap.xml", 'w');
  34. fwrite($fp, $sitemap);
  35. fclose($fp);
  36.  
  37. }
  38.  
  39. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.