Limit available sites in Multisite Language Switcher
Published: – Leave a comment
By default, Multisite Language Switcher displays all configured sites in a multisite as potential translation. This is not useful in certain environments. For instance, here at Epiphyt, we have the main site in English and German as well as the site of Impressum Plus. This means, we have four sites in total, of which two are in English and two are in German.
Mutlisite Language Switcher don’t allow you to link sites to each other but assumes that every configured site in a multisite contains a different translation for the same content. In our case it means that the same content is available two times in English and two times in German. Besides being dumb it also produces several problems using Multisite Language Switcher. For instance, you cannot properly update linked pages because two of them use the same identifier, which always is the language identifier.
To workaround this, we searched for a way to limit the configured sites in Multisite Language Switcher depending on the current domain so that the Epiphyt site only knows the English and German version of Epiphyt and the Impressum Plus site only knows the English and German version of Impressum Plus.
Fortunately, Multisite Language Switcher has the filter msls_blog_collection_construct
to achieve that:
/**
* Filter the blog collection of Multisite Language Switcher.
*
* @param array $collection The current collection
* @return array The filtered collection
*/
function my_filter_msls_collection( array $collection ): array {
foreach ( $collection as $blog_id => $blog ) {
if (
$_SERVER['HTTP_HOST'] === 'epiph.yt' && strpos( $blog->siteurl, 'epiph.yt' ) === false
|| $_SERVER['HTTP_HOST'] === 'impressum.plus' && strpos( $blog->siteurl, 'impressum.plus' ) === false
) {
unset( $collection[ $blog_id ] );
}
}
return $collection;
}
add_filter( 'msls_blog_collection_construct', 'my_filter_msls_collection' );
Code language: PHP (php)
The filter allows you to modify the blog collection, which means all sites in the multisite that are configured in Multisite Language Switcher. If the current URL contains the host epiph.yt
and the current blog in the collection doesn’t contain epiph.yt
, it will be removed from the collection. The same applies to impressum.plus
.
That way, I can use the same multisite for two different sites and both being translated in English and German.