Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Yoast SEO: Create a custom sitemap with data not sourced from a custom post type.
<?php
/**
* USAGE:
* - Search and replace the `CUSTOM_SITEMAP` string with a lowercase identifier name: e.g., "vehicles", "customer_profiles"
* - The `your_alternate_data_source()` function does not exist; it's simply a placeholder for your own function that returns some sort of data array.
* - Uses heredocs for inline XML: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
*/
/**
* Uncomment the next line of code to disable sitemap caching.
* - For development environments and debugging only.
* - All other scenarios, follow the "Manual Sitemap Update" instructions in the Yoast documentation:
* - https://yoast.com/help/sitemap-does-not-update/#manual
*/
#add_filter("wpseo_enable_xml_sitemap_transient_caching", "__return_false");
/**
* Add CUSTOM_SITEMAP-sitemap.xml to Yoast sitemap index
*/
function CUSTOM_SITEMAP_sitemap_index($sitemap_index) {
global $wpseo_sitemaps;
$sitemap_url = home_url("CUSTOM_SITEMAP-sitemap.xml");
$sitemap_date = date(DATE_W3C); # Current date and time in sitemap format.
$custom_sitemap = <<<SITEMAP_INDEX_ENTRY
<sitemap>
<loc>%s</loc>
<lastmod>%s</lastmod>
</sitemap>
SITEMAP_INDEX_ENTRY;
$sitemap_index .= sprintf($custom_sitemap, $sitemap_url, $sitemap_date);
return $sitemap_index;
}
add_filter("wpseo_sitemap_index", "CUSTOM_SITEMAP_sitemap_index");
/**
* Register CUSTOM_SITEMAP sitemap with Yoast
*/
function CUSTOM_SITEMAP_sitemap_register() {
global $wpseo_sitemaps;
if (isset($wpseo_sitemaps) && !empty($wpseo_sitemaps)) {
$wpseo_sitemaps->register_sitemap("CUSTOM_SITEMAP", "CUSTOM_SITEMAP_sitemap_generate");
}
}
add_action("init", "CUSTOM_SITEMAP_sitemap_register");
/**
* Generate CUSTOM_SITEMAP sitemap XML body
*/
function CUSTOM_SITEMAP_sitemap_generate() {
global $wpseo_sitemaps;
$data = your_alternate_data_source(); # Replace this with your own data source function
$urls = array();
foreach ($data as $item) {
$urls[]= $wpseo_sitemaps->renderer->sitemap_url(array(
"mod" => $item->date, # <lastmod></lastmod>
"loc" => $item->url, # <loc></loc>
"images" => array(
array( # <image:image></image:image>
"src" => $item->image_url, # <image:loc></image:loc>
"title" => $item->image_title, # <image:title></image:title>
"alt" => $item->image_title, # <image:caption></image:caption>
),
),
));
}
$sitemap_body = <<<SITEMAP_BODY
<urlset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
%s
</urlset>
SITEMAP_BODY;
$sitemap = sprintf($sitemap_body, implode("\n", $urls));
$wpseo_sitemaps->set_sitemap($sitemap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.