is_sitemap()
is_sitemap
Determines whether the current request is for a WordPress XML sitemap.
Checks the sitemap index and individual provider sitemaps.
Sitemap XSL stylesheet requests are not included: /wp-sitemap.xsl and /wp-sitemap-index.xsl.
This conditional tag works only after the main query has been parsed—see wp(). If called too early, it triggers a warning and returns false.
Uses: WP_Query::is_sitemap()
No Hooks.
Returns
true|false.
true— the request is for an XML sitemap.false— the request is not for a sitemap, or the main query has not been created yet.
Usage
if ( is_sitemap() ) {
// your sitemap code
}
Examples
#1 Check an XML sitemap request
This simple example shows how to add a custom HTTP header for a sitemap.
add_action( 'template_redirect', 'wp_kama_handle_sitemap_request' );
function wp_kama_handle_sitemap_request() {
if ( ! is_sitemap() ) {
return;
}
header( 'X-WP-Sitemap: 1' );
}
#2 Check an XML sitemap and its XSL stylesheets
Includes XML sitemaps and the /wp-sitemap.xsl and /wp-sitemap-index.xsl requests.
add_action( 'template_redirect', 'wp_kama_handle_all_sitemap_requests' );
function wp_kama_handle_all_sitemap_requests() {
$is_stylesheet = (bool) get_query_var( 'sitemap-stylesheet' );
if ( ! is_sitemap() && ! $is_stylesheet ) {
return;
}
header( 'X-WP-Sitemap: 1' );
}Notes
- Global. WP_Query.
$wp_queryWordPress Query object.
Changelog
| Since 7.1.0 | Introduced. |
is_sitemap() is sitemap code WP 7.1
function is_sitemap(): bool {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '7.1.0' );
return false;
}
return $wp_query->is_sitemap();
}