Remember language choice on multisite website


I have a multisite website with 3 websites representing 3 languages. On my network website I have a page with 3 language choices linking to, and

The first time a user gets to I want them to choose their language, redirect to the corresponding subsite and store that choice in a cookie. Next time the visitor goes to he should be redirected automatically to the subsite stored in the cookie. Obviously if someone goes directly to nothing should happen.

I tried adding this code in the functions.php in my child-theme, but it doesn’t work at all.
What am I doing wrong?

// if a cookies is set and the user is on, redirect to the saved language subsite
if (isset($_COOKIE['language'])) {
    $pagename = $_SERVER['REQUEST_URI'];
    if ($pagename == '/') {

// if the user choose a language save a cookie
if (isset($_GET["language"])) {
    setcookie('language', 1, time() + 1209600, "/", "".$_GET['language']."", false); // Set the chosen language
    wp_safe_redirect("".$_GET['language']); // redirect to the chosen language

Thanks a lot!


netcult 2 months 0 Answers 10 views 0

Leave an answer