Description of the environment: one Wordpress multisite with the root site example.com and multiple subdomains like en.example.com, es.example ...
Question: how to get the user to the right subdomain based on the language of the browser?
Plugins found until now: Wordpress redirect whom description contains the following:
WordPress plugin which redirects from the root site of a multisite project to a language specific network site.
Please Note: This plugin doesn’t allow content on the root site! Please read this description carefully to see if this works for you.
it seems that this redirection plugin is for redirection to subfolders, I am a newbie to wordpress and it is quite long time ago where I have done something with PHP, but it should not be so complicated I guess..
Your architecture is wrong, WordPress multisite is not used for language translation unless you want to have an entirely different design/theme for your other language, you should use a plugin like Polylang (Free) instead, you can use subdomains if you want!
But just to answer your question, if you continue using your current multisite architecture you would use a function like this:
function redirectOtherLanguages(){
if(!isReturningVisitor())
{
$domain = getDomain();
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$newAddress = 'http://'.$lang.'.'.$domain.$_SERVER['REQUEST_URI'];
echo $newAddress; die();
header("Location: ".$newAddress);//include check session FR
}
}
function getDomain(){
$parts=explode('.', $_SERVER["SERVER_NAME"]);
$potentialSubDomain = array_shift($parts);
//print_r($parts); die();
if(!empty($parts)){
if(count($parts)>=2) return implode('.',$parts);
else return $_SERVER["SERVER_NAME"];
}
else return null;
}
//This function prevents redirecting returning users
function isReturningVisitor(){
if(empty($_COOKIE['returning_visitor']))
{
setcookie("returning", TRUE, time()+60*60*24*365); // Expires in a year
return false;
}
return true;
}
redirectOtherLanguages();
Here are some other problems you will encounter: