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 == '/') {
wp_safe_redirect("/".$_COOKIE['language']);
exit;
}
}
// 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
exit;
}
UPDATE This was the final code that worked:
if (isset($_COOKIE['language'])) {
$pagename = $_SERVER['REQUEST_URI'];
if ($pagename == '/') {
wp_safe_redirect("/".$_COOKIE['language']);
exit;
}
}
if (isset($_GET["language"])) {
$language = sanitize_text_field($_GET["language"]);
setcookie('language', $language, time() + 1209600, "/", "example", false);
}
Thanks a lot!
Mark
I have a multisite website with 3 websites representing 3 languages. On my network website https://example I have a page with 3 language choices linking to https://example/nl?language=nl, https://example/fr?language=fr and https://example/en?language=en.
The first time a user gets to https://example 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 https://example he should be redirected automatically to the subsite stored in the cookie. Obviously if someone goes directly to https://example/nl/... 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 https://example/, redirect to the saved language subsite
if (isset($_COOKIE['language'])) {
$pagename = $_SERVER['REQUEST_URI'];
if ($pagename == '/') {
wp_safe_redirect("https://example/".$_COOKIE['language']);
exit;
}
}
// if the user choose a language save a cookie
if (isset($_GET["language"])) {
setcookie('language', 1, time() + 1209600, "/", "https://example/".$_GET['language']."", false); // Set the chosen language
wp_safe_redirect("https://example/".$_GET['language']); // redirect to the chosen language
exit;
}
UPDATE This was the final code that worked:
if (isset($_COOKIE['language'])) {
$pagename = $_SERVER['REQUEST_URI'];
if ($pagename == '/') {
wp_safe_redirect("/".$_COOKIE['language']);
exit;
}
}
if (isset($_GET["language"])) {
$language = sanitize_text_field($_GET["language"]);
setcookie('language', $language, time() + 1209600, "/", "example", false);
}
Thanks a lot!
Mark
Share Improve this question edited Nov 24, 2020 at 21:53 netcult asked Nov 23, 2020 at 21:37 netcultnetcult 32 bronze badges1 Answer
Reset to default 0setcookie()
has the following signature:
setcookie ( $name, $value = "", $expires = 0, $path = "", $domain = "", $secure = FALSE, $httponly = FALSE )
You're setting the cookie's value to 1
in your code. I suspect you intend to do something more like
setcookie('language', $_GET['language'], time() + 1209600, "/", "example", false);
Notes
- This answer doesn't address the security issues in trusting that the
$_GET
data is clean. You should research data sanitization. - WordPress provides a bunch of handy constants to make something like
time() + 1209600
easier to read. You can do something liketime() + ( 2 * WEEK_IN_SECONDS )
, for example.