Skip to content

Instantly share code, notes, and snippets.

@7ute
Created January 26, 2023 10:33
Show Gist options
  • Save 7ute/0ae53b65db64e2752a516e9186a1c744 to your computer and use it in GitHub Desktop.
Save 7ute/0ae53b65db64e2752a516e9186a1c744 to your computer and use it in GitHub Desktop.
Yoast custom sitemap provider
<?php
/**
* These class and hook allow exposing your data to Yoast, for it to build
* the sitemap, split it correctly in multiple pages every 1000 entries,
* and add it to its index. Here's a list of the placeholders I used :
* - MY_NAMESPACE: your Plugin/theme/custom namespace
*
* - MY_CUSTOM_DATATYPE: The type of data you're exposing in those sitemaps
* - MY_CUSTOM_DATATABLE: Database table hosting your data
* - MY_MOD_DATE_COLUMN: Last modified date column from the aforementionned table
* - <YOUR CUSTOM DATA COLUMNS>: Get your data here…
* - <URL TO THE PAGE>: …and use it to create the pages URIs in your sitemap
*
* You can refer to the Taxonomy sitemap provider
* included in Yoast to get more details about
* how Yoast handles these provider classes.
* @see /wp-content/plugins/wordpress-seo/inc/sitemaps/class-taxonomy-sitemap-provider.php
*
* Another approach, without sitemap provider classes:
* @link https://gist.github.com/leepowers/2b32f734571cbeadc811b93501cfcced
*/
class MY_NAMESPACE_Sitemap_Provider implements WPSEO_Sitemap_Provider
{
public function handles_type($type)
{
if ($type == 'MY_CUSTOM_DATATYPE') {
return true;
}
return false;
}
public function get_index_links($max_entries)
{
global $wpdb;
$all_items = $wpdb->get_results("SELECT MY_MOD_DATE_COLUMN FROM MY_CUSTOM_DATATABLE");
$index = [];
$max_pages = 1;
$total_count = count($all_items);
if ($total_count > $max_entries) {
$max_pages = (int) ceil($total_count / $max_entries);
}
for ($page_counter = 0; $page_counter < $max_pages; $page_counter++) {
$current_page = ($page_counter === 0) ? '' : ($page_counter + 1);
$items = array_splice($all_items, 0, $max_entries);
$last_mod = '2000-01-01 00:00:00';
if (!$items) {
continue;
}
foreach ($items as $item) {
$last_mod = max($last_mod, $item->MY_MOD_DATE_COLUMN);
}
$index[] = [
'loc' => WPSEO_Sitemaps_Router::get_base_url('MY_CUSTOM_DATATYPE-sitemap' . $current_page . '.xml'),
'lastmod' => $last_mod,
];
}
return $index;
}
public function get_sitemap_links($type, $max_entries, $current_page)
{
global $wpdb;
$links = [];
$offset = ($current_page > 1) ? (($current_page - 1) * $max_entries) : 0;
$all_items = $wpdb->get_results(
"SELECT <YOUR CUSTOM DATA COLUMNS>, MY_MOD_DATE_COLUMN
FROM MY_CUSTOM_DATATABLE
LIMIT {$offset}, {$max_entries}"
);
foreach ($all_items as $item) {
$links[] = [
"loc" => '<URL TO THE PAGE>',
"mod" => $item->MY_MOD_DATE_COLUMN,
];
}
return $links;
}
}
/**
* Here's the hook to tell Yoast to add your external provider to the list
* @see /wp-content/plugins/wordpress-seo/inc/sitemaps/class-sitemaps.php:127
*/
function MY_NAMESPACE_sitemap_providers()
{
return [
new MY_NAMESPACE_Sitemap_Provider(),
// ...Put your other sitemap providers here
];
}
add_filter('wpseo_sitemaps_providers', 'MY_NAMESPACE_sitemap_providers');
/**
* Uncomment this line in development environment to disable the sitemap caching.
*/
// add_filter("wpseo_enable_xml_sitemap_transient_caching", "__return_false");
@armansargsyan1
Copy link

cuz im trying but its not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment