Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by Daniel 7 years, 1 month ago.

Assigned support staff: Bruno.

Author Posts
June 18, 2013 at 3:24 pm #136923


Hi there, I am trying to introduce language redirection by accessing geolocation.
I am using ip2location for this and get the country code in the end, which works fine.
Then I check, if the current site language corresponds to the users country and redirect, if not with following code:

global $sitepress;
if($sitepress->get_current_language() != $country_language_code){
  $languages = $sitepress->get_ls_languages();
    wp_safe_redirect($languages[$country_language_code]['url'], 301);
  } else{
    wp_safe_redirect(home_url(), 301);


When I now try to alter the language_code in the url lets say from en to de, wordpress goes into a infinite loop because it tries to fetch an url in the format of

I hook my redirect function into add_action( 'template_redirect', 'wpml_geo_location_redirect');

How can I make wordpress to still look for and then check with the users geolocation if he is allowed to access that page and if not redirect him back to ?

June 18, 2013 at 3:49 pm #136932


Dear Daniel,

Please, replace this line:

wp_safe_redirect(home_url(), 301);

by this:

wp_safe_redirect(site_url(), 301);

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Thank you.

June 18, 2013 at 4:57 pm #136949


Thanks for your quick reply. Unfortunately, that does not prevent wordpress from going into a loop and producing a /lang/lang url, when manually changing the lang parameter in the url...

June 18, 2013 at 7:17 pm #136975


Dear Daniel,

Please, try this one:

wp_safe_redirect(site_url('/'), 301);

Please let me know if this solves the problem.

June 18, 2013 at 8:59 pm #136998


Hi Bruno
I ended up using the geo redirect plugin ( and i got it to work.
thanks for your assistance.