Skip to content

Instantly share code, notes, and snippets.

@dirkeinecke
Last active December 2, 2019 13:24
Show Gist options
  • Save dirkeinecke/ea6a9a5a41e641a95bafc486d5ffde58 to your computer and use it in GitHub Desktop.
Save dirkeinecke/ea6a9a5a41e641a95bafc486d5ffde58 to your computer and use it in GitHub Desktop.
Wordpress - Create sitemap.xml without PlugIn
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>'.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