GeoIP Browser Language Redirect for WPML

logo

I needed to send users reliably to the correct WPML translation depending on the country they came from. The “Browser language redirect” option that WPML provides is a very simple method using the HTTP_ACCEPT_LANGUAGE header. It doesn’t work reliably, and assumes you have named your languages after proper locale names.

GeoIP Redirector for WPML to the rescue!

It uses a GeoIP database (MaxMind) to pinpoint user location more exactly. It also lets you specify your own mappings (user country to language) easily.

Feel free to fork the plugin on GitHub, or download it directly. (Please read README.md for instructions on setting the plugin up.)

35 thoughts on “GeoIP Browser Language Redirect for WPML

  1. Ana

    Does this still work. I installed in on my website but still redirection does not work. Can you help me?

    Reply
  2. Ana

    I have tried to debug it and I found out that the code execution does not reach inside the if if(intval(get_query_var(‘wpml_geoip’)) == 1)
    {

    I don’t know why, can you help me please?

    Reply
    1. Stanislav Khromov Post author

      Hi Ana,

      It should still work, but there are a few edge cases where you might have issues.

      A few questions:

      • What version of WPML are you using?
      • Can you check if you have any JavaScript errors? (F12 for debug console in Chrome/Firefox)
      • If you enter jQuery.cookie in the JS Console, what does it return? It should look like in this screenshot

      The way it works is by checking for a cookie via a JavaScript. If the cookie does not exist, an AJAX call is made which leads to the line in your comment being triggered. If the JavaScript does not work, no redirect can be made.

      Let me know if that helps.

      Reply
  3. Pingback: GeoIP Browser Language Redirect for WPML - Daniel Hansson

    1. Stanislav Khromov Post author

      HI Stavros,

      Did you configure the plugin properly?
      You have to go into the PHP file “WPML_GeoIP_IPResolver.class.php” and configure your language mappings. (Row 35 in the code.)

      If you have configured the mapping, could you check the javascript error log in your browser, as something might be wrong with the javascript. (WPML changes it around quite often.)

      You can send me the error log and more info by creating an issue on Github.

      Hope that helps.

      Reply
  4. Stavros Kefaleas

    Here I paste the javascript errors I am getting :

    12:55:19.335 Use of getUserData() or setUserData() is deprecated. Use WeakMap or element.dataset instead. requestNotifier.js:64
    12:55:22.538 Use of Mutation Events is deprecated. Use MutationObserver instead. script-compiler.js:617
    12:55:24.819 Use of getPreventDefault() is deprecated. Use defaultPrevented instead. jquery.js:4
    12:56:25.463 The Web Console logging API (console.log, console.info, console.warn, console.error) has been disabled by a script on this page.

    Reply
    1. Stanislav Khromov Post author

      Stavros,

      They are not errors, just deprecation notices. They are not related to this plugin.

      Reply
  5. Juanma

    Hi Stanislav,

    Your plugin works perfectly, thanks so much. I just have one problem: it’s impossible to navigate through the site in another language different to your country. My site is in Spanish and I can’t navigate in English for example, automatically it’s redirected to the Spanish version.

    Am I doing anything wrong?

    Thanks so much in advance,
    Juanma

    Reply
  6. Juanma

    I mean: I’d like to enter in the site redirected automatically to my language but after that I’d like to have the possibility of changing it (as the WPML works without this plugin )

    Reply
  7. Duncan

    Hi there

    Does your plugin work with custom WPML langauges?

    Also, does it work when on localhost?

    Thanks in advance

    Reply
    1. Stanislav Khromov Post author

      Hey Duncan,

      It does work with any WPML language, but you need to configure the plugin for your needs by modifying the language mappings inside the PHP file, so there is some programming required.

      Here is the exact position you need to edit:
      https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/blob/master/WPML_GeoIP_IPResolver.class.php#L34

      As for using it on localhost, the plugin will simply be unable to tell your location, so it will default to the default language. (Which you also configure in the plugin.)

      Reply
  8. Niels

    Hi There,

    just installed the plugin on our website. Simple redirect: all visitors should go to the /nl website except US visitors: they should go to the /en website.
    Configured it as follows (activated and also the correct WPML Language setting is on: always translate)
    //Array with structure MaxMind Code => WPML Code
    $this->language_mappings = array(
    ‘US’ => ‘en’, //USA

    );

    //Set the default WPML language which is used if no matching language is found
    $this->default_language = ‘nl’;

    Any ideas on why it’s not working? Using WPM: 3.1.8.4.
    Thanks

    Reply
  9. ahmed

    Im using your plugin and it works great, but is there anyway to configure it so that it remembers the user’s language preference and give that priority over the country based redirect?

    Reply
    1. Stanislav Khromov Post author

      Hey ahmed,

      Right now, the plugin will redirect the user to his language based on GeoIP on the first pageload. (Using a cookie to keep track if user has already been redirected or not.)

      After that, the user is free to change his language to anything he wants, but there is no way to force the user to another language after that. This is by design to avoid potential redirect loops.

      Reply
      1. ahmed

        i was able to store the user’s preferred language in a cookie but i still cant figure out how to redirect the user to that language. Is it possible to add an extra piece of code to the plugin in the part that redirects the user so that it first checks if the preferred language cookie exists or not and if it does, redirects to the preferred language instead of the geoIP redirect?

        if so could you please point me to the part in the plugin that i need to configure?

        Reply
        1. Stanislav Khromov Post author

          ahmed,

          I’m not sure that would work…

          If the “preferred language” cookie is set after the user has visited the site once, (Maybe by clicking on a language switcher.), then it wont work because GeoIP Redirector only redirects fresh visitors on their very first pageload. So after the first page load, this plugin does nothing.

          It may work if you set the cookie in the PHP response from WordPress, but I don’t think that is what you had in mind?

          Here is where we check for the cookie that shows if the user was already redirected:
          https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/blob/master/js/browser-redirect-geoip.js#L41

          Here is where the redirect takes place:
          https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/blob/master/js/browser-redirect-geoip.js#L103

          But remember, the code runs only on the first pageload.

          Reply
  10. Marco

    Hi Stanislav,

    the ip redirect is not working. Debug:

    browser-redirect-geoip.js:23 Uncaught
    Uncaught TypeError: Cannot read property ‘log’ of undefined

    Thanks
    Marco

    Reply
  11. Vincent

    Hi Stanislav,

    Does your plugin work on a WPMU site?
    We’ve installed it and it works on the main domain but doesn’t seem to work on the subdomains.

    Any clues?

    Vincent

    Reply
    1. Stanislav Khromov Post author

      Hey Vincent,

      I don’t see why it wouldn’t work on subdomains.

      There’s a flag called GEOIP_DEBUG in the main javascript file that you can enable to print out useful info to console:
      https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/blob/master/js/browser-redirect-geoip.js#L3

      The, check the issue list on GitHub and open your own if you can’t find anything related to your problem:
      https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/issues

      Hope that helps.

      Reply
  12. ashish

    hi guys, finally after a long struggle on this , I have found the solution, that will run wpml with geoip webservices, the language will change according to user ip location. :) if you want the know how to configure it using custom code. just mail me at [snipped]

    Reply
    1. Stanislav Khromov Post author

      Hey ashish,

      Sorry, no advertising via email in the comment section. Feel free to link to your website though.

      Reply
  13. Darjan

    We are currently testing your plugin a bit, but we have some problems with exceptions. We have a ecommerce site which redirects paypal/zapier calls.

    Is it possible to have an exception added? Something like:

    public function checkIfRedirectNeeded()
    {
    // CANCEL redirect for requests containing following in URL:
    if (strpos($this->request_uri, “paypal”) > 0) { return; }
    if (strpos($this->request_uri, “zapier”) > 0) { return; }

    if (!is_array($this->geo_redirect_data))
    return;

    But how/where would we connect that?

    Reply
    1. Stanislav Khromov Post author

      Hey Darjan,

      All the redirect logic takes place in JavaScript, in order to be compatible with page caching.

      You can change the redirect logic in the main javascript file:
      https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/blob/master/js/browser-redirect-geoip.js#L44

      You should be able to use window.location.href to get the current URL and then redirect based on that.

      If you set the GEOIP_DEBUG variable to true at the top of the file you will also get all kinds of info regarding the redirect logic in the javascript console.

      Hope that helps.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Markdown is allowed in comments.