Skip to content

Emit hreflang x-default when more than one translation exists#641

Open
apermo wants to merge 2 commits into
lloc:masterfrom
apermo:feature/hreflang-x-default
Open

Emit hreflang x-default when more than one translation exists#641
apermo wants to merge 2 commits into
lloc:masterfrom
apermo:feature/hreflang-x-default

Conversation

@apermo
Copy link
Copy Markdown
Contributor

@apermo apermo commented May 31, 2026

Closes #640.

What

MslsOutput::get_alternate_links() only emitted an hreflang="x-default" alternate when a
single translation existed. With two or more translations — the normal multilingual case —
x-default was dropped and only language-specific alternates were returned.

x-default is the fallback search engines use when no other hreflang matches the visitor's
locale; Google and the W3C recommend it for multilingual pages. This emits it in the
multi-alternate case too.

How

  • In the multi-alternate branch, run the already-computed $default through the existing
    msls_output_get_alternate_links_default filter and prepend it to the alternates.
  • Returning '' from that filter suppresses x-default (opt-out). Single-translation behaviour
    is unchanged.

Tests

  • Updated test_get_alternate_links_two_url to assert the x-default alternate is present.
  • Added test_get_alternate_links_x_default_suppressed covering the empty-string opt-out.
  • phpunit green, phpcs (PHPCompatibility) clean, phpstan reports no new errors in
    MslsOutput.

Refs

get_alternate_links() only returned an x-default alternate when a
single translation existed; with two or more it was dropped. x-default
is the recommended fallback for multilingual pages, so emit it in the
multi-alternate case too.

The x-default link runs through the existing
msls_output_get_alternate_links_default filter, so it stays
customisable and can be suppressed by returning an empty string.
Single-translation behaviour is unchanged.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates get_alternate_links in includes/MslsOutput.php to apply the MSLS_ALTERNATE_LINKS_DEFAULT_HOOK filter and prepend the default link to the alternate links array if it is not empty, accompanied by new PHPUnit tests to verify this behavior. The review feedback suggests adding an early return if $arr is empty to prevent applying the filter and outputting a lone x-default link when no other alternate links are available.

Comment thread includes/MslsOutput.php
Address review: bail out before applying the default filter when the
alternates array is empty, so an empty page can't emit a lone
x-default link.
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.

Emit hreflang x-default when more than one translation exists

1 participant