/* 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 = ''; $sitemap .= "\n" . '' . "\n"; foreach( $postsForSitemap as $post ) { setup_postdata( $post ); $postdate = explode( " ", $post->post_modified ); $sitemap .= "\t" . '' . "\n" . "\t\t" . '' . get_permalink( $post->ID ) . '' . "\n\t\t" . '' . $postdate[0] . '' . "\n\t\t" . 'monthly' . "\n\t" . '' . "\n"; } $sitemap .= ''; $fp = fopen( ABSPATH . "sitemap.xml", 'w' ); fwrite( $fp, $sitemap ); fclose( $fp );