WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. openmtl
    Member
    Posted 6 months 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'])) {
    			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.

    http://wordpress.org/plugins/language-redirect/

  2. openmtl
    Member
    Posted 6 months 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...
    Member
    Posted 4 months 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
    Member
    Posted 2 months 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
    Member
    Posted 1 month 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
    Member
    Plugin Author

    Posted 1 month 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.

Reply

You must log in to post.

About this Plugin

About this Topic