Language Redirect
Adding cookie to make redirection once-only (have code) (6 posts)

  1. openmtl
    Posted 2 years ago #

    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'])) {
    		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>
    						<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>

    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.


  2. openmtl
    Posted 2 years ago #

    added code to a patch on github https://github.com/lincolnphipps/language-redirect Will be adding more pages to my subsites and if it all looks ok will do a pull request.

  3. let me see...
    Posted 1 year ago #

    openmtl, thank you for your fix. However, I will not use it, since I would exlude users with cookies disabled. I want everyone to access my sites, if they want to.

  4. grayayer
    Posted 1 year ago #

    Adding these modifications seems to work for. Doesn't seem like you committed those to your forked version on github though. Am I missing something?

    An additional feature that would improve UX would be to display upon first redirect a message that the user has been auto redirected based on their browser language preference and provide a link to the main site if they don't want to be on the subsite.

  5. grayayer
    Posted 1 year ago #

    I'm now receiving a problem in which I realize that because the first thing that a server sees is a 302 redirect message, it's impossible to define open graph meta-tags to post content to Facebook and have it automatically populate.

    if I don't use the store cookie method, I seem to be encountering a infinite redirect loop when just trying to go to my English site.

  6. bjoerne
    Plugin Author

    Posted 1 year ago #

    If you yourself post something on Facebook you could use the redirect target instead of your homepage. For example if you post on an English Facebook page you could use http://www.example.com/en/.

    But even if you mention your homepage on Facebook, Facebook follows the redirect and uses the meta tags of the redirect target.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Language Redirect
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic