WP_Sitemaps
Main sitemap controller integrating all sitemap components.
Source: wp-includes/sitemaps/class-wp-sitemaps.php
Since: 5.5.0
Overview
The main class that orchestrates sitemap functionality including provider registration, rewrite rules, rendering, and robots.txt integration.
Properties
| Property | Type | Visibility | Description |
|---|---|---|---|
$index | WP_Sitemaps_Index | public | Sitemap index generator |
$registry | WP_Sitemaps_Registry | public | Provider registry |
$renderer | WP_Sitemaps_Renderer | public | XML renderer |
Methods
__construct()
Initializes sitemap components.
public function __construct()Creates:
WP_Sitemaps_RegistryinstanceWP_Sitemaps_RendererinstanceWP_Sitemaps_Indexinstance (with registry)
init()
Initiates all sitemap functionality.
public function init(): voidActions:
- Registers rewrite rules (always, for proper 404 handling)
- Adds
template_redirectaction for rendering - If sitemaps enabled:
- Registers default providers
- Adds sitemap to robots.txt
Note: Called automatically by wp_sitemaps_get_server().
sitemaps_enabled()
Determines whether sitemaps are enabled.
public function sitemaps_enabled(): boolReturns: true if sitemaps are enabled.
Default: Enabled when blog_public option is true.
Hooks:
apply_filters( 'wp_sitemaps_enabled', bool $is_enabled )Example:
// Disable sitemaps entirely
add_filter( 'wp_sitemaps_enabled', '__return_false' );register_sitemaps()
Registers default sitemap providers.
public function register_sitemaps(): voidRegisters:
- Registers default providers
- Adds sitemap to robots.txt
register_rewrites()
Registers sitemap rewrite tags and routing rules.
public function register_rewrites(): voidRewrite Tags:
- Registers default providers
- Adds sitemap to robots.txt
Rewrite Rules:
| Pattern | Query |
|---|---|
^wp-sitemap.xml$ | sitemap=index |
^wp-sitemap.xsl$ | sitemap-stylesheet=sitemap |
^wp-sitemap-index.xsl$ | sitemap-stylesheet=index |
^wp-sitemap-([a-z]+?)-([a-zd_-]+?)-(d+?).xml$ | Provider + subtype + page |
^wp-sitemap-([a-z]+?)-(d+?).xml$ | Provider + page |
render_sitemaps()
Renders sitemap templates based on rewrite rules.
public function render_sitemaps(): voidHooked to: template_redirect
Flow:
- Check query vars (
sitemap,sitemap-subtype,sitemap-stylesheet,paged) - If not sitemap request, return early
- If sitemaps disabled, return 404
- For stylesheet: render via
WP_Sitemaps_Stylesheet - For index: render via
WP_Sitemaps_Renderer::render_index() - For provider: get URLs and render via
WP_Sitemaps_Renderer::render_sitemap() - If no URLs found, return 404
add_robots()
Adds the sitemap index to robots.txt.
public function add_robots( string $output, bool $is_public ): stringHooked to: robots_txt filter (priority 0)
| Parameter | Type | Description |
|---|---|---|
$output | string | Current robots.txt content |
$is_public | bool | Whether site is public |
Returns: Modified robots.txt with sitemap URL.
Output:
Sitemap: https://example.com/wp-sitemap.xmlredirect_sitemapxml()
Redirects /sitemap-xml to /wp-sitemap.xml.
public function redirect_sitemapxml( bool $bypass, WP_Query $query ): boolDeprecated: 6.7.0 — Use WP_Rewrite::rewrite_rules() instead.
| Parameter | Type | Description |
|---|---|---|
$bypass | bool | Pre_handle_404 filter value |
$query | WP_Query | Query object |
Returns: Pass-through of bypass value.
Usage Example
// Get sitemap server
$sitemaps = wp_sitemaps_get_server();
// Access components
$providers = $sitemaps->registry->get_providers();
$index_url = $sitemaps->index->get_index_url();
// Check if enabled
if ( $sitemaps->sitemaps_enabled() ) {
// Sitemaps are active
}