This function here probably does the same as your example:
add_filter( 'msls_head_hreflang', function ( $language ) {
$map = array(
'en_US' => 'en_us',
'en-gb' => 'en',
'es-es' => 'es',
'fr' => 'x-default',
);
return $map[ $language ] ?? $language;
} );
In the same function exists another filter called “mlsl_output_get_alternate_links_arr” which expects the array with the generated links as input:
add_filter( 'mlsl_output_get_alternate_links_arr', function ( $links ) {
// do something with the array of links
return $links;
} );
My website tags are
<link rel=”alternate” hreflang=”de” href=”https://www.dentaly.org/de/” title=”de_DE” /><link rel=”alternate” hreflang=”en” href=”https://www.dentaly.org/en/” title=”en_GB” /><link rel=”alternate” hreflang=”en_us” href=”https://www.dentaly.org/us/” title=”en_US” /><link rel=”alternate” hreflang=”es” href=”https://www.dentaly.org/es/” title=”es_ES” /><link rel=”alternate” hreflang=”fr” href=”https://www.dentaly.org/” title=”fr_FR” />
Chatgpt recomend this
<link rel=”alternate” hreflang=”de-DE” href=”https://www.dentaly.org/de/” /> <link rel=”alternate” hreflang=”en-GB” href=”https://www.dentaly.org/en/” /> <link rel=”alternate” hreflang=”en-US” href=”https://www.dentaly.org/us/” /> <link rel=”alternate” hreflang=”es-ES” href=”https://www.dentaly.org/es/” /> <link rel=”alternate” hreflang=”fr-FR” href=”https://www.dentaly.org/” />
How we can achive this?
Hi Any update from your end?
I gave you an example of a rewrite of your function. What you really want to archive is probably something like this:
add_filter( 'msls_head_hreflang', function ( $language ) {
return str_replace( '_', '-', $language );
} );
The plugin itself applies the following logic though: If the language code contains the region, but the language is exclusive (like German in your example) then the country code will be removed because it is redundant.
Thank You. If i need to remove the title tag from hreflang how we can achieve this?
Title is no needed.
<link rel=”alternate” hreflang=”en-GB” href=”https://www.dentaly.org/en/” title=”en_GB” /><link rel=”alternate” hreflang=”en-US” href=”https://www.dentaly.org/us/” title=”en_US” /><link rel=”alternate” hreflang=”es” href=”https://www.dentaly.org/es/” title=”es_ES” /><link rel=”alternate” hreflang=”fr-FR” href=”https://www.dentaly.org/” title=”fr_FR” />
The title might not be needed, but is an excepted attribute: https://www.w3schools.com/tags/tag_link.asp
You could use the ‘mlsl_output_get_alternate_links_arr’ to clean it, but I don’t think it is worth it. You may make a feature request, if you still believe, that an additional filter is needed: https://github.com/lloc/Multisite-Language-Switcher/issues