Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


GeoIP Browser Language Redirect for WPML

Stanislav KhromovStanislav Khromov

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.)

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 36
  • Ana
    Posted on

    Ana Ana

    Reply Author

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


  • Ana
    Posted on

    Ana Ana

    Reply Author

    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?


  • Stavros Kefaleas
    Posted on

    Stavros Kefaleas Stavros Kefaleas

    Reply Author

    I believe that your plugin has stopped working.
    Any ideas?


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply 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.


  • Stavros Kefaleas
    Posted on

    Stavros Kefaleas Stavros Kefaleas

    Reply Author

    I am gonna look right now for errors.
    What is strange is that the plug-in worked fine for me for at least a month, but suddenly it stopped working.
    Here is my file : http://pastebin.com/fuvyuNvj

    PS : Thank you very much for your fast answer! :)


  • Stavros Kefaleas
    Posted on

    Stavros Kefaleas Stavros Kefaleas

    Reply Author

    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.


  • Stavros Kefaleas
    Posted on

    Stavros Kefaleas Stavros Kefaleas

    Reply Author

    I am using WPML Multilingual CMS Version 3.0.2-a


  • Stavros Kefaleas
    Posted on

    Stavros Kefaleas Stavros Kefaleas

    Reply Author

    Do these javascript errors have to do with your plugin or they are irrelevant?


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Stavros,

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


  • Stavros Kefaleas
    Posted on

    Stavros Kefaleas Stavros Kefaleas

    Reply Author

    Can I give you any more information so that I can help you find what is wrong?


  • Juanma
    Posted on

    Juanma Juanma

    Reply Author

    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


  • Juanma
    Posted on

    Juanma Juanma

    Reply Author

    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 )


  • Duncan
    Posted on

    Duncan Duncan

    Reply Author

    Hi there

    Does your plugin work with custom WPML langauges?

    Also, does it work when on localhost?

    Thanks in advance


  • Niels
    Posted on

    Niels Niels

    Reply Author

    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


  • ahmed
    Posted on

    ahmed ahmed

    Reply Author

    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?


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply 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.


      • ahmed
        Posted on

        ahmed ahmed

        Reply Author

        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?


  • Marco
    Posted on

    Marco Marco

    Reply Author

    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


  • Vincent
    Posted on

    Vincent Vincent

    Reply Author

    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


  • ashish
    Posted on

    ashish ashish

    Reply Author

    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]


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Hey ashish,

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


  • Darjan
    Posted on

    Darjan Darjan

    Reply Author

    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?


  • Saish
    Posted on

    Saish Saish

    Reply Author

    Hi,

    I am using WPML version 4.1.3 on my website. I have installed the wonderful plugin you developed on my website. However, it doesn’t seem to work for me. Is it still compatible? Below attached is my setting for the plugin