Skip to content

fix(TranslatePress): remove ignored query params from hreflang tags#8119

Open
faisalahammad wants to merge 2 commits intowp-media:developfrom
faisalahammad:fix/8111-translatepress-hreflang-query-params
Open

fix(TranslatePress): remove ignored query params from hreflang tags#8119
faisalahammad wants to merge 2 commits intowp-media:developfrom
faisalahammad:fix/8111-translatepress-hreflang-query-params

Conversation

@faisalahammad
Copy link

@faisalahammad faisalahammad commented Mar 4, 2026

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_buffer that 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:

public static function get_subscribed_events() {
    return [
        'rocket_saas_is_home_url'                      => [ 'detect_homepage', 10, 2 ],
        'rocket_has_i18n'                              => 'is_translatepress',
        // ... other events
        'rocket_current_url'                           => 'adjust_current_url',
    ];
}

After:

public static function get_subscribed_events() {
    return [
        'rocket_saas_is_home_url'                      => [ 'detect_homepage', 10, 2 ],
        'rocket_has_i18n'                              => 'is_translatepress',
        // ... other events
        'rocket_current_url'                           => 'adjust_current_url',
        'rocket_buffer'                                => [ 'clean_hreflang_query_strings', 1000 ],
    ];
}

Why: Hooks into the buffer processing to clean hreflang tags before the HTML is cached.

New Methods Added

public function clean_hreflang_query_strings( $buffer ) {
    $ignored_params = rocket_get_ignored_parameters();

    if ( empty( $ignored_params ) ) {
        return $buffer;
    }

    $pattern = '/<link\s+[^>]*hreflang=["\'][^>]*>/i';

    return preg_replace_callback(
        $pattern,
        function ( $matches ) use ( $ignored_params ) {
            $tag     = $matches[0];
            $cleaned = $this->remove_ignored_params_from_href( $tag, $ignored_params );
            return $cleaned;
        },
        $buffer
    );
}

Why:

  • Uses rocket_get_ignored_parameters() to respect the global ignored parameters list
  • Regex targets only <link> tags with hreflang attribute
  • Processes each tag to clean the href attribute
private function clean_url_query_string( $url, $ignored_params ) {
    $parsed_url = wp_parse_url( $url );

    if ( ! isset( $parsed_url['query'] ) ) {
        return $url;
    }

    wp_parse_str( $parsed_url['query'], $query_params );
    $query_params = array_diff_key( $query_params, array_flip( $ignored_params ) );
    $cleaned_query = http_build_query( $query_params );

    if ( empty( $cleaned_query ) ) {
        // Rebuild URL without query string
        return $base_url;
    }

    // Rebuild URL with cleaned query string
    return $new_url;
}

Why: Properly rebuilds URLs after removing ignored parameters, handling edge cases like ports and fragments.

Testing

Test 1: Remove single ignored parameter

  • Visit page with ?trp-edit-translation=1
  • Check cached HTML hreflang tags
  • Result: Parameter removed from all hreflang links

Test 2: Remove multiple ignored parameters

  • Visit page with ?trp-edit-translation=1&utm_source=test
  • Both parameters in ignored list
  • Result: Both parameters removed

Test 3: Preserve allowed parameters

  • Visit page with ?trp-edit-translation=1&product=123
  • Only trp-edit-translation ignored
  • Result: trp-edit-translation removed, product=123 preserved

Test 4: Non-hreflang links unaffected

  • Page contains stylesheet and other link tags
  • Result: Only hreflang tags modified

Unit Tests:

$ composer test-unit -- --filter=TranslatePress
............                                                      12 / 12 (100%)
OK (12 tests, 12 assertions)

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
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.

TranslatePress - Avoid writing URLs with parameters into hreflang tags

1 participant