Skip to content

Strip <link rel="alternate"> from global header REST output - #734

Merged
dd32 merged 5 commits into
trunkfrom
copilot/remove-alternate-link-headers
Mar 9, 2026
Merged

dd32 merged 5 commits into
trunkfrom
copilot/remove-alternate-link-headers

Conversation

Copilot AI commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor

The global header REST endpoint includes wp_head() output for styles, which also emits <link rel="alternate"> tags (feed links, oEmbed discovery, hreflang from plugins, etc.) that are irrelevant when the header is embedded in other sites. The previous approach used remove_action on known core hooks, which missed tags added by plugins.

  • Replaced remove_action calls (feed_links, feed_links_extra, wp_oembed_add_discovery_links) with a regex-based strip on the rendered $head_markup, catching all sources
  • Added remove_head_alternate_links() — strips <link> tags where rel contains the alternate token, applied only during REST requests
function remove_head_alternate_links( $markup ) {
	return preg_replace( '/<link\b[^>]*\brel=["\'][^"\']*\balternate\b[^"\']*["\'][^>]*\/?>/i', '', $markup );
}

Word boundary (\b) prevents false matches on values like rel="myalternate".

  • Added PHPUnit tests (phpunit/test-global-header-footer.php) covering removal of RSS/Atom feed links, oEmbed discovery, hreflang tags, single-quoted and non-self-closing variants, compound rel values, preservation of non-alternate links (stylesheet, canonical, preconnect, icon), mixed markup, and edge cases.
Original prompt

This section details on the original issue you should resolve

<issue_title>Don't output <link rel="alternate" in the headers</issue_title>
<issue_description>In https://meta.trac.wordpress.org/changeset/14677 I removed the <link rel="alternate" tags from the Trac templates, but doesn't remove it from other things (404/403/codex/etc)

The Global Header output however shouldn't have these tags at all.

We remove the feed links, but we don't remove the tags added by plugins.

We should use WP_HTML_Tag_Parser to remove these from the output.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Remove feed_links, feed_links_extra, and wp_oembed_add_discovery_links
from wp_head when rendering the global header via REST API. These
<link rel="alternate"> tags are not relevant for the reusable header
component served to other sites.

Co-authored-by: dd32 <767313+dd32@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove <link rel="alternate"> tags from Global Header output Remove <link rel="alternate"> from global header REST endpoint output Mar 9, 2026
Copilot finished work on behalf of dd32 March 9, 2026 04:52
…lobal header output

Replace the remove_action approach (which only handled known core actions)
with WP_HTML_Tag_Processor-based filtering that catches <link rel="alternate">
tags from any source, including plugins. Uses token-based rel attribute
matching to avoid false positives with non-alternate rel values.

Co-authored-by: dd32 <767313+dd32@users.noreply.github.com>
Copilot AI changed the title Remove <link rel="alternate"> from global header REST endpoint output Strip <link rel="alternate"> from global header REST output using WP_HTML_Tag_Processor Mar 9, 2026
Copilot finished work on behalf of dd32 March 9, 2026 05:01
Replace the two-step WP_HTML_Tag_Processor + regex approach with a
single regex that directly matches and removes <link> tags whose
rel attribute contains the word "alternate".

Co-authored-by: dd32 <767313+dd32@users.noreply.github.com>
Copilot AI changed the title Strip <link rel="alternate"> from global header REST output using WP_HTML_Tag_Processor Strip <link rel="alternate"> from global header REST output Mar 9, 2026
Copilot finished work on behalf of dd32 March 9, 2026 05:08
@dd32
dd32 marked this pull request as ready for review March 9, 2026 05:13
@dd32

dd32 commented Mar 9, 2026

Copy link
Copy Markdown
Member

@copilot Can you add any tests for this?

Co-authored-by: dd32 <767313+dd32@users.noreply.github.com>

Copilot AI commented Mar 9, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Can you add any tests for this?

Added PHPUnit tests in 06421af. The test file covers: removing RSS/Atom feed links, oEmbed discovery, hreflang tags, single-quoted and non-self-closing variants, compound rel values, and preserving non-alternate links (stylesheet, canonical, preconnect, icon). Also tests mixed markup and edge cases (empty input, no link tags).

Copilot finished work on behalf of dd32 March 9, 2026 05:21
@dd32
dd32 merged commit f284971 into trunk Mar 9, 2026
2 checks passed
@dd32
dd32 deleted the copilot/remove-alternate-link-headers branch March 9, 2026 05:35
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.

Don't output <link rel="alternate" in the headers

2 participants