Skip to content

Sitemaps: Demonstrate adding HTTP headers to sitemap responses with existing hooks - #16108

Draft
desrosj wants to merge 1 commit into
WordPress:trunkfrom
desrosj:60552-sitemap-headers-tests
Draft

desrosj wants to merge 1 commit into
WordPress:trunkfrom
desrosj:60552-sitemap-headers-tests

Conversation

@desrosj

@desrosj desrosj commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

This adds tests showing that the existing wp_headers filter and send_headers action, combined with is_sitemap(), can add a header such as X-Robots-Tag to core sitemap responses without the wp_doing_sitemaps action proposed in #12044.

They pass on trunk because of how requests are handled now:

  • Since r54250, WP::main() runs query_posts() before send_headers(), so is_sitemap() (r62664) works inside both hooks.
  • Both hooks fire before template_redirect, which is where WP_Sitemaps::render_sitemaps() sends Content-Type and the XML.
  • r64073 removed the sitemap stylesheet routes, so every sitemap response core still serves is one where is_sitemap() is true.

The new Tests_Sitemaps_SendHeaders class covers:

  • Sitemap routes: a wp_headers callback adds X-Robots-Tag on the index, posts (post, page, paged), taxonomies and users sitemaps.
  • Other pages: the header is not added to the home page.
  • send_headers: is_sitemap() is true inside the send_headers action (the approach in comment:6).
  • Ordering: wp_headers fires before any sitemap entry is rendered, and the response is a 200 with <urlset> output.
  • Caveat: is_sitemap() is also true for sitemap routes that render_sitemaps() later turns into a 404, such as an unregistered provider. The action proposed in #60552 - Add wp_doing_sitemaps action hook to WP_Sitemaps::render_sitemaps() #12044 fires before the provider, subtype and page checks, so it has the same behavior.

To check that the tests depend on this ordering, I temporarily moved send_headers() back in front of query_posts(). With that change, 9 of the 10 tests fail.

The tests can only check the headers passed through wp_headers and the order of the hooks, because PHP's header() has no effect under the CLI.

Trac ticket: https://core.trac.wordpress.org/ticket/60552

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Analysis of the ticket and confirming the core request flow, writing the tests and this description; reviewed by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

🤖 Generated with Claude Code

Add tests demonstrating that a `wp_headers` filter or `send_headers` action callback can use `is_sitemap()` to add a header, such as `X-Robots-Tag`, to core sitemap responses. `WP::main()` runs `query_posts()` before `send_headers()`, so the conditional tag works there, and both hooks fire before `WP_Sitemaps::render_sitemaps()` runs on `template_redirect`.

The tests also document that `is_sitemap()` is true for sitemap routes that `render_sitemaps()` later turns into a 404.

Follow-up to r54250, r62664, r64073.

See #60552.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desrosj desrosj self-assigned this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant