fix(TranslatePress): remove ignored query params from hreflang tags#8119
Open
faisalahammad wants to merge 2 commits intowp-media:developfrom
Open
fix(TranslatePress): remove ignored query params from hreflang tags#8119faisalahammad wants to merge 2 commits intowp-media:developfrom
faisalahammad wants to merge 2 commits intowp-media:developfrom
Conversation
When TranslatePress generates hreflang tags, it may include query parameters that should be ignored during caching. This causes cached pages to contain hreflang tags with parameters like trp-edit-translation, which should not appear in the final output. The fix hooks into rocket_buffer to clean hreflang link tags by: - Finding all link tags with hreflang attribute - Removing any query parameters that are in the ignored parameters list - Preserving allowed query parameters Fixes wp-media#8111
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes hreflang tags generated by TranslatePress containing ignored query parameters (like
?trp-edit-translation=1) in cached pages.Fixes #8111
Problem
When a page is visited with query parameters that WP Rocket ignores during caching (e.g.,
?trp-edit-translation=1), TranslatePress generates hreflang tags containing those parameters. The cached HTML then includes these parameters in hreflang links, even though they should be stripped.Solution
Added a filter on
rocket_bufferthat processes hreflang link tags and removes any query parameters that are in the ignored parameters list. The fix runs late in the buffer processing (priority 1000) to ensure all hreflang tags have been generated.Changes
inc/ThirdParty/Plugins/I18n/TranslatePress.php
Before:
After:
Why: Hooks into the buffer processing to clean hreflang tags before the HTML is cached.
New Methods Added
Why:
rocket_get_ignored_parameters()to respect the global ignored parameters list<link>tags withhreflangattributeWhy: Properly rebuilds URLs after removing ignored parameters, handling edge cases like ports and fragments.
Testing
Test 1: Remove single ignored parameter
?trp-edit-translation=1Test 2: Remove multiple ignored parameters
?trp-edit-translation=1&utm_source=testTest 3: Preserve allowed parameters
?trp-edit-translation=1&product=123trp-edit-translationignoredtrp-edit-translationremoved,product=123preservedTest 4: Non-hreflang links unaffected
Unit Tests: