Emit hreflang x-default when more than one translation exists#641
Open
apermo wants to merge 2 commits into
Open
Emit hreflang x-default when more than one translation exists#641apermo wants to merge 2 commits into
apermo wants to merge 2 commits into
Conversation
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.
Contributor
There was a problem hiding this comment.
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.
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.
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.
Closes #640.
What
MslsOutput::get_alternate_links()only emitted anhreflang="x-default"alternate when asingle translation existed. With two or more translations — the normal multilingual case —
x-default was dropped and only language-specific alternates were returned.
x-defaultis the fallback search engines use when no otherhreflangmatches the visitor'slocale; Google and the W3C recommend it for multilingual pages. This emits it in the
multi-alternate case too.
How
$defaultthrough the existingmsls_output_get_alternate_links_defaultfilter and prepend it to the alternates.''from that filter suppresses x-default (opt-out). Single-translation behaviouris unchanged.
Tests
test_get_alternate_links_two_urlto assert the x-default alternate is present.test_get_alternate_links_x_default_suppressedcovering the empty-string opt-out.phpunitgreen,phpcs(PHPCompatibility) clean,phpstanreports no new errors inMslsOutput.Refs