Last active
August 10, 2018 10:14
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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