lloc / Multisite-Language-Switcher

WordPress Plugin: Multisite Language Switcher
http://msls.co
GNU General Public License v2.0
134 stars 41 forks source link

Regarding the "x-default" is not working properly #240

Open polydecking opened 1 year ago

polydecking commented 1 year ago

Firstly thanks very much for your work of this great plugin!

The issue is regarding the "x-default" tag.

I checked that the x-default is used to specify the default language version that should be selected when there is no language matching the user's preference. In this way, when the other language versions of the web page do not meet the user's language preference, the search engine and browser will choose the x-default version as a backup option. It helps ensure that users get the most appropriate content when they don't explicitly specify a preference or don't have a preferred language version.

According to above defination, I fould 2 problems regarding the x-default,

  1. First When I was using msls, I found that only when the current page is not connected to other sites of other multi-site systems, the x-default tag will be added to the header; If the page connect to related page in different language, it doesn't show the x-default, this is contrary to the way x-default is used. Websites in all languages ​​should be marked with the x-default tag in the multilingual system.

  2. Secondly, the default site is the English site, but the German site generated x-default tag is showing the German site url, that is, link rel="alternate" hreflang="x-default" href="http://xxx.com/de/" title="German" , which is not the url of the main site(english site), this is contrary to the way x-default is used. The x-default should use the link rel="alternate" href="https://xxx.com/" hreflang="x-default" in each different language sites, since the english site is the main site.

In fact, no matter which language site you are in, x-default should be the language of the main site, no matter you are in german site or french site or mainsite, the x-default should be the main site which is english site url, like below, the top 2 lines with bold text should be there: link rel="alternate" href="https://xxx.com/" hreflang="x-default" link rel="alternate" href="https://xxx.com/" hreflang="en" link rel="alternate" href="https://xxx.com/de" hreflang="es" link rel="alternate" href="xxx.com/fr" hreflang="fr"

Then I checked Kinsta's multilingual website, it was build by multisite too. All their different language website's hrefland tags start with: link rel="alternate" href="https://kinsta.com/blog/wordpress-multilingual/" hreflang="x-default" link rel="alternate" href="https://kinsta.com/blog/wordpress-multilingual/" hreflang="en" link rel="alternate" href="https://kinsta.com/es/blog/wordpress-multilingue/" hreflang="es" link rel="alternate" href="https://kinsta.com/fr/blog/wordpress-multilingue/" hreflang="fr"

The above are the issues I found when using it, I hope it can help the plugin to be perfect.

Again, thanks very much~

DarkBBC commented 2 weeks ago

Hi! I solved this problem this way: Edit the file /includes/MslsOutput.php We are looking for public function get_alternate_links() $arr[] = sprintf( $format, esc_attr( $hreflang ), esc_url( $url ), esc_attr( $description ) ); After this line, insert the code:

if ($hreflang == 'en') {
$format = sprintf( $format, 'x-default', esc_url( $url ), esc_attr( $description ) );
$arr[] = sprintf( $format, esc_attr( $hreflang ), esc_url( $url ), esc_attr( $description ) );
}

The code checks if there is an EN language, then it additionally displays a tag with hreflang="x-default".