Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dirkeinecke/6bde9b171b5f3e11d24a39b490457af1 to your computer and use it in GitHub Desktop.
Save dirkeinecke/6bde9b171b5f3e11d24a39b490457af1 to your computer and use it in GitHub Desktop.
Wordpress - Create sitemap.xml without PlugIn (Version for MAMP PRO -> Remote -> Publish Host)
add_action('save_post', 'create_sitemap');
function create_sitemap() {
$items = (array) get_posts(array(
'numberposts' => -1,
'post_type' => (array) array(
(string) 'post',
(string) 'page',
),
'post_status' => (string) 'publish',
));
$sitemap = (string) '';
$sitemap .= '<?xml version=\'1.0\' encoding=\'UTF-8\'?>';
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach ($items as $item) {
$sitemap .= '<url>';
$sitemap .= ' <loc>'.str_replace('http://LOCAL_HOSTNAME/', 'https://REMOTE_HOSTNAME/', get_permalink($item->ID)).'</loc>';
$sitemap .= ' <lastmod>'.get_the_modified_date('Y-m-d', $item->ID).'</lastmod>';
$sitemap .= ' <changefreq>daily</changefreq>';
$sitemap .= ' <priority>1</priority>';
$sitemap .= '</url>';
}
$sitemap .= '</urlset>';
file_put_contents(ABSPATH.'sitemap.xml', $sitemap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment