Skip to content

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

@apermo

Description

@apermo

Problem

MslsOutput::get_alternate_links() only outputs an hreflang="x-default" alternate when
exactly one linked translation exists:

if ( 1 === count( $arr ) ) {
    return apply_filters( self::MSLS_ALTERNATE_LINKS_DEFAULT_HOOK, $default );
}

$arr = (array) apply_filters( self::MSLS_ALTERNATE_LINKS_ARR_HOOK, $arr );

return implode( PHP_EOL, $arr );

When two or more translations exist — the normal multilingual case — x-default is dropped and
only the language-specific alternates are returned.

x-default is the fallback search engines use when no other hreflang matches the visitor's
locale (typically a language selector or the default-region page). Google and the W3C recommend
including it for multilingual pages, so omitting it precisely when several languages exist is the
opposite of the recommended behaviour.

Proposed fix

In the multi-alternate branch, also emit the already-computed $default (x-default) link, run
through the existing msls_output_get_alternate_links_default filter so it stays customisable
and can be suppressed by returning an empty string. Additive and backward compatible.

Acceptance criteria

  • A page with ≥2 translations outputs an hreflang="x-default" alternate alongside the
    language-specific ones.
  • Returning '' from msls_output_get_alternate_links_default suppresses x-default.
  • Single-translation behaviour is unchanged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions