I'm writing a plug-in to redirect visitors to the translated page if they have a German IP.

  add_action( 'wpml_loaded', 'geo_redirect_client' );
    function geo_redirect_client() {
        define( 'IP_LANG', 'de' ); // This will be coming from an some IP to country code API
        global $sitepress;
        if( ICL_LANGUAGE_CODE != IP_LANG ) {
            $languages = $sitepress->get_ls_languages();
            if( isset( $languages[IP_LANG] ) ) {
                $url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
                $redirect_url = apply_filters( 'wpml_permalink', $url, IP_LANG );
                //wp_redirect( $redirect_url, 301 );
       echo $redirect_url;

But the issue is wpml_permalink is not right.

Instead of getting www.mysite.com/de/kontakt/ it's getting www.mysite.com/de/contact/

  • This appears to be a known bug in wpml(possible solution in the last reply). Forcefully redirecting users at every pageload is a terrible idea. What if someone is currently in Germany (on a trip) but doesn't speak german? What if they simply prefer another language? Maybe redirect on first hit, but please allow visitors to switch languages afterwards!
    – Iceable
    Feb 7 '18 at 8:26
  • @Iceable Thanks for the reply. It's per my client request. I understand that the IP redirection is not a very good idea and having some drawbacks. I will discuss with him about these issues later. However, the above issues can be solved by using template_redirect action hook. Feb 7 '18 at 9:04

For anyone who is having a similar issue. You'll be able to solve the issue by using wp, wp_head, wp_footer or template_redirect action hooks.

In this case, template_redirect action hook solved my issue.

add_action( 'template_redirect', 'redirect_visitor' );
function redirect_visitor() {
    // Do your stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.