WordPress System Redirection to Home /lang

Description

This is one of the first codes of this 2021. Specifically, it adds to Wordpress the function of redirecting the system to the home page of the same language as the web browser or, failing that, to the homepage version in English. This code is only activated once (control by cookies) and is only activated if accessed through the homepage. It also has another controller to disable the redirection function to facilitate the access of crawler bots from Google, Yahoo, Bing, Yandex, Amazon, Facebook ...

Code

add_action('wp_loaded', 'my_icl_set_current_language');           

function my_icl_set_current_language() {

if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")
   OR strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "bingbot")
   OR strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "slurp")
   OR strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "yandexBot")
   OR strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "facebot")
   OR strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "ia_archiver")
  ) {

} else { 

if ($_SERVER['REQUEST_URI'] == '/' OR $_SERVER['REQUEST_URI'] == '/es/' OR $_SERVER['REQUEST_URI'] == '/en/' OR $_SERVER['REQUEST_URI'] == '/ru/' OR $_SERVER['REQUEST_URI'] == '/fr/') {   

if ( isset( $_COOKIE['UserLanguage']) ){

} else {

$lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);     

setcookie("UserLanguage", $lang, time() + 86400, "/");
setcookie("UserLanguage", $lang, time() + 86400, "/es/");
setcookie("UserLanguage", $lang, time() + 86400, "/fr/");
setcookie("UserLanguage", $lang, time() + 86400, "/ru/");
setcookie("UserLanguage", $lang, time() + 86400, "/en/");

if (($lang=="it_IT") OR ($lang=="it-IT"))  {
    wp_redirect( "/" );
    exit();
}else if (($lang=="es_ES") OR ($lang=="es-ES"))  {
    wp_redirect( "/es/" );
    exit();
}else if (($lang=="fr_FR") OR ($lang=="fr-FR"))  {
    wp_redirect( "/fr/" );
    exit();
}else if (($lang=="de_DE") OR ($lang=="ru-RU"))  {
    wp_redirect( "/de/" );
    exit();
}else {
    wp_redirect( "/en/" );
    exit();
}                     
}

} else { }

}
}