Skip to content

Instantly share code, notes, and snippets.

@alexmoise
Last active February 18, 2018 13:30
Show Gist options
  • Save alexmoise/66921b63fe6efe541eeb094118d1720a to your computer and use it in GitHub Desktop.
Save alexmoise/66921b63fe6efe541eeb094118d1720a to your computer and use it in GitHub Desktop.
A custom plugin to output the years sitemap along the Yoast sitemaps, for Andy Myers Lodge
<?php
/**
* Plugin Name: AML Years XML Sitemap
* Plugin URI: https://gist.github.com/alexmoise/66921b63fe6efe541eeb094118d1720a
* GitHub Plugin URI: https://gist.github.com/alexmoise/66921b63fe6efe541eeb094118d1720a
* Description: A custom plugin to output the years sitemap along the Yoast sitemaps, for Andy Myers Lodge
* Version: 1.0.2
* Author: Alex Moise
* Author URI: https://moise.pro
*/
// Add the sitemap to Yoast sitemaps index
add_filter( 'wpseo_sitemap_index', 'AML_sitemap_custom_items' );
function AML_sitemap_custom_items() {
$sitemap_custom_items = '
<sitemap>
<loc>'.get_home_url().'/years-sitemap.xml</loc>
<lastmod>2017-05-22T23:12:27+00:00</lastmod>
</sitemap>
';
return $sitemap_custom_items;
}
// === Now about the sitemap ===
// On publish of post, update sitemap
add_action("publish_post", "AML_years_sitemap_gen");
// Update of page
add_action("publish_page", "AML_years_sitemap_gen");
// If revised post/page
add_action("save_post", "AML_years_sitemap_gen");
// On activation, create sitemap
register_activation_hook( __FILE__, 'AML_years_sitemap_gen');
function AML_years_sitemap_gen() {
global $Gallery;
$homepath = get_home_path();
// Create XML
$sitemap = '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//'.parse_url(get_home_url(), PHP_URL_HOST).parse_url(get_home_url(), PHP_URL_PATH).'/main-sitemap.xsl"?>';
$sitemap .= "\n".'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
foreach($Gallery->archives_years() as $year) {
$sitemap .= "\t".'<url>'."\n".
"\t\t".'<loc>'. $year['archive_link'] .'</loc>'.
"\n\t\t".'<changefreq>monthly</changefreq>'.
"\n\t".'</url>'."\n";
}
$sitemap .= '</urlset>';
$fp = fopen($homepath . "years-sitemap.xml", 'w');
fwrite($fp, $sitemap);
fclose($fp);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment