WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
Enhance feature "When front page is visited, set language according to browser" (7 posts)

  1. Ov3rfly
    Member
    Posted 1 year ago #

    Another small suggestion which fixes one unavailable functionality if no front end cookie is used.

    If feature "When the front page is visited, set the language according to the browser preference" is enabled, and user clicks a "Home" link to front page in a a non-browser preference language, and has no cookie set, he gets to the browser preference language front page.

    That's ok, a known limiation of the "no cookie" expert setting.

    With a small enhancement, this could be taken care of:

    File include/core.php, line 144

    Now:

    if ($this->options['browser']) {

    Suggestion: Also checks for "no referer?" or "referer, but not site-internal link"

    if ($this->options['browser'] && (!isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], home_url()) === false) ) {

    What about that?

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Yes. That would be good to improve the no cookie way of working. However, I would do the test before in the process. In include/core.php, line 399, replace:

    $this->curlang = $this->options['hide_default'] && (isset($_COOKIE['wordpress_polylang']) || isset($_COOKIE[PLL_COOKIE])) ?
    	$this->get_language($this->options['default_lang']) :
    	$this->get_preferred_language(); // sets the language according to browser preference or default language

    by:

    $this->curlang = $this->options['hide_default'] && ((isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $this->home) !== false) || isset($_COOKIE['wordpress_polylang']) || isset($_COOKIE[PLL_COOKIE])) ?
    	$this->get_language($this->options['default_lang']) :
    	$this->get_preferred_language(); // sets the language according to browser preference or default language
  3. Ov3rfly
    Member
    Posted 1 year ago #

    Thanks, your version works fine and you are right, it's better to add it there in line 399.

  4. leods92
    Member
    Posted 1 year ago #

    @Chouby
    I'm not completely sure if this is a new bug but using the lastest stable version of your plugin I have one problem: users are not arriving at the right homepage.
    Going through the latest SVN development changes, I noticed you have made some changes related to this but even trying the latest development versions the problem persists.

    Just to assure we are on the same page here, I'd like to summarize how this feature should work:
    - User accesses homepage without pll cookie (new visitor or visitor who has disabled cookies) -> Polylang gets the preferred language from request HTTP header "Accept-Language", sets the proper cookie, and if the language is different from the homepage's, redirects the user.
    - User with pll cookie accesses the homepage -> Polylang gets the preferred language from cookies, and if the language is different from the homepage's, redirects the user.
    - User deliberately change the language, by clicking on a link inside the website -> Polylang reassigns the cookie value and DOES NOT redirect the user as he/she really wants to be at a different homepage.

    Since the function get_preferred_language() takes care of getting the preferred language from the cookie (if available), the code you've just proposed and the one that's currently being used should not evaluate whether (isset($_COOKIE['wordpress_polylang']) || isset($_COOKIE[PLL_COOKIE]) is true. Doing so, will skip get_preferred_language and if the user tries to access the homepage WITH a cookie different from the homepage language, he/she won't be redirected and the cookie will be reassigned to the homepage's language code.

    I'm not sure if I was clear; if I wasn't please let me know so I explain it better.
    I've removed the unnecessary evaluation from core.php in my blog and the redirections are now working as expected.

    Hope yo hear your insights about this issue.

  5. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    I believe I understand what you mean. In current stable version, I do not test the referer so cases 2 and 3 are mixed and the test of the cookie is here because of the case 3.

    Finally I believe that now these two cases are separate, your proposal makes sense.

  6. leods92
    Member
    Posted 1 year ago #

    @Chouby
    Exactly!
    And the problem of not testing the referer is only noticeable when you hide the default language prefix from the URL.

  7. Ov3rfly
    Member
    Posted 1 year ago #

    @leods92: Thanks for tracking this down.

    Did not mention in my first post with include/core.php referer check suggestion that also the setting "hide the default language prefix" was used.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags