Software Developer

Add URL to sitemap.xml When Scheduled Post is Published

This snippet will do this: when a scheduled post is published in your WordPress site, its URL will be added automatically to your “sitemap.xml” file. This assumes that your “sitemap.xml” file is located at the root of your site.

//Add URL to sitemap when scheduled post is published

function scheduled_post_published($post) {
	$f = $_SERVER['DOCUMENT_ROOT'] . '/sitemap.xml';
	$fo = fopen($f, 'r+');
	$i = '<url><loc>' . get_permalink($post) . '</loc><lastmod>'. date('c') .'</lastmod><changefreq>yearly</changefreq><priority>0.7</priority></url></urlset>';
	fseek($fo, -9, SEEK_END);
	fwrite($fo, $i);
	fclose($fo);
}
add_action('future_to_publish', 'scheduled_post_published', 10, 1);

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]