There is no is_sitemap()
core function in WP < 6.8, but there is a ticket for it here.
The rewrite rules for the sitemap (src) use e.g. the sitemap
query variable:
add_rewrite_rule( '^wp-sitemap\.xml$', 'index.php?sitemap=index', 'top' );
add_rewrite_rule(
'^wp-sitemap-([a-z]+?)-([a-z\d_-]+?)-(\d+?)\.xml$',
'index.php?sitemap=$matches[1]&sitemap-subtype=$matches[2]&paged=$matches[3]',
'top'
);
add_rewrite_rule(
'^wp-sitemap-([a-z]+?)-(\d+?)\.xml$',
'index.php?sitemap=$matches[1]&paged=$matches[2]',
'top'
);
The get_query_var( 'sitemap' )
is checked within the template_redirect
hook to see if the sitemap should be rendered (among other things, like if the sitemap is enabled) (src).
An untested suggestion would be to check if get_query_var( 'sitemap' )
returns a truthy string according to the check made in core (src):
if ( ! function_exists( 'is_sitemap' ) ) {
function is_sitemap() : bool {
$sitemap = sanitize_text_field( get_query_var( 'sitemap' ) );
return (bool) $sitemap;
}
}
I also skipped my previous a-z+
check on $sitemap
, as the core check does only care about if it’s boolish, after running it through sanitize_text_field()
.
We could also add to the function, a similar check for the XSL stylesheet with the sitemap-stylesheet
query variable.
It might be safer to prefix the function’s name as well.
But in general it should be possible to use instead existing sitemap hooks, to target a specific sitemap problem, rather than creating a specific is_sitemap()
function for it.