Adding cookie to make redirection once-only (have code)
-
I have a multisite with the root as the default english language and then /fr/ and /it/ subsites. So if the current redirect plugin is used then a visitor will always get sent to the /fr/ or /it/ subsites even if they wanted to stay on the english default.
The fix I have come up with is to set a cookie so if the user has visited then the redirect action is skipped.
The code is trivial …
In language-redirect.php I added in to language_redirect_register_settings()
register_setting('language_redirect_group', 'language_redirect_use_has_visited_cookie');
And in function language_redirect_plugins_loaded() about line 34,
if (get_option('language_redirect_use_has_visited_cookie') == "1") { if (isset($_COOKIE['languageredirect_hasvisted'])) { return; } if (!isset($_COOKIE['languageredirect_hasvisted'])) { setcookie('languageredirect_hasvisted', 1, time()+2419200, COOKIEPATH, COOKIE_DOMAIN, false); } }
And in options.php I added the extra setting,
<tr valign="top"> <th scope="row">Set and use visit cookie</th> <td> <input id="language_redirect_use_has_visited_cookie" name="language_redirect_use_has_visited_cookie" class="checkbox" type="checkbox" value="1" <?= checked( get_option('language_redirect_use_has_visited_cookie'), 1, false );?> /> <p class="description">Used to stop redirect if user has already visited site</p> </td> </tr>
That’s a 4 week timer for the cookie.
The behaviour now is that the redirect will work once when the client hits the root site i.e. a French browser will go to e.g. /fr/ but if they go back to the root i.e. / then the redirect action will not then redirect them back to /fr/. After 28 days (the cookie timer I have used though it could be anything), the browser will redirect just once again if they visit the root site.
- The topic ‘Adding cookie to make redirection once-only (have code)’ is closed to new replies.