Multisite-Language-Switcher icon indicating copy to clipboard operation
Multisite-Language-Switcher copied to clipboard

Adding filter to customize ahrelang tags

Open jacksoggetto opened this issue 3 years ago • 0 comments

I did an online search, and I'm not the only one to have this need.

for example someone here asked about x-default: https://wordpress.org/support/topic/is-it-possible-to-set-x-default-when-using-multisite-language-switcher/

adding a filter would help, I added this line:

$arr = apply_filters( 'mlsl_output_hreflang', $arr );

on line 124 of the file multisite-language-switcher/includes/MslsOutput.php

This will allow everyone to manipulate the output it can actually be done better, by returning $arr instead of putting the if in the return, and change the code a bit, so that the return is 100% customizable.

I'm more than happy to give you soem help with this if you wish.

Then with this code we can manipulate however we wish:

function mlsl_output_edit_hreflang( $arr ) {

	if ( count($arr) == 1)
		return;
	
	if ( get_msls_permalink( 'en_US' ) != '' )
		array_unshift($arr, '<link rel="alternate" hreflang="x-default" href="' . get_msls_permalink( 'en_US' ) . '" />');
	
	return $arr;

}
add_filter( 'mlsl_output_hreflang', 'mlsl_output_edit_hreflang' );

With this code I add an x-default when needed and remove hreflang tags where there are no translations

jacksoggetto avatar Jul 12 '22 17:07 jacksoggetto