khromov / wp-wpml-geoip-browser-language-redirect

GeoIP Redirector for WPML (WordPress)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Redirection not working

383creative opened this issue · comments

Installed plugin and changed language mapping arrays. Testing from a GB IP Address and I should have been redirected to /gb language code but no redirection takes place. Site hosted with Pantheon.

image

image

image

Turned the debug to true and get the following message in console.

image

Hi,

This looks a little crazy, console.log is a standard function yet it's not available here. Can you try using a different browser, and if that doesn't work, can you try using a standard theme and disable any plugins you are running. (Alternatively, try running this plugin on a clean WP install with Twenty Sixteen, WPML and this plugin only and see if that resolves the issue.)

Ok so since we're on Pantheon it was easy for me to spin up a clean WP install. All I installed was WPML and your plugin.

image

WPML Languages Enabled:

image

Still seem to be getting the console error for log.

Chrome:

image

Firefox also returned an error:

image

I was able to get the console.log stuff to print by removing the IF statements.

image

As you can see, it thinks I'm in the US. I might be way off on this but I believe it's because Pantheon/Varnish doesn't return the clients true IP true, unless you use ($_SERVER['REMOTE_ADDR']) to get the IP?

See this thread on a similar plugin - https://wordpress.org/support/topic/geotargeting-problem/

@383creative Sounds like it. What is the response if you type the following URL in your browser: http://example.com/?wpml_geoip=1 - it should tell you which country the plugin thinks you're in.

The plugin first checks for $_SERVER['HTTP_X_FORWARDED_FOR'] (which is what a caching proxy would typically use) and then falls back to $_SERVER['REMOTE_ADDR'].

You can try checking with Pantheon what they actually use. If they use another header, we can add it in to support it.

Ahh thanks for the ?wpml_geoip=1 trick. It shows me as country code en (which is not the case). In order to get the correct IP what should we change? Is it this part in wpml-geoip-browser-language-redirect.php?

function register_endpoints()
	{
		if(intval(get_query_var('wpml_geoip')) == 1)
		{
			include('WPML_GeoIP_IPResolver.class.php');
			$ipr = new WPML_GeoIP_IPResolver();

			$ipr->set_json_header();
			if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
				$tmp_ip_array = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
				$tmp_ip = $tmp_ip_array[0];
			} else {
				$tmp_ip = $_SERVER['REMOTE_ADDR'];
			}
			echo $ipr->ip_to_wpml_country_code($tmp_ip);
			die();
		}
	}

I did this:

`
function register_endpoints()
{
if(intval(get_query_var('wpml_geoip')) == 1)
{
include('WPML_GeoIP_IPResolver.class.php');
$ipr = new WPML_GeoIP_IPResolver();

		$ipr->set_json_header();

			$tmp_ip = $_SERVER['REMOTE_ADDR'];
		
		echo $ipr->ip_to_wpml_country_code($tmp_ip) . '<br>';
		echo $_SERVER['REMOTE_ADDR'];
		die();
	}
}

}
`
And it returns the correct IP address, but still says EN. Perhaps the mappings are outdated?

@383creative Might be. What is the IP in question (you can hide the last octet if you want.)

Hi @khromov

We really appreciate your plugin to solve ip location redirect, we also encounter this same issue yet we were able to resolve this. As our gratitude to your plugin please see what we updated on your code, hope this helps you in maintaining your plugin.

On wpml-geoip-browser-language-redirect.php, we updated the following:

  1. On function __construct, we replace your action print_script to enqueue_scripts -- see below

/** Initialize and add actions */
function __construct()
{
//Init script
add_action( 'wp_enqueue_scripts',array($this, 'enqueue_scripts'), 10 );
//add_action('wp_print_scripts', array(&$this, 'enqueue_scripts'), 100);

  1. we had added register_script action on your function enqueue_scripts

/** Unload old browser redirect and add new one **/
function enqueue_scripts()
{
global $sitepress, $sitepress_settings;

	//De-register old script
	wp_deregister_script('wpml-browser-redirect');

	//Register new one
	wp_register_script('jquery.cookie', ICL_PLUGIN_URL . '/res/js/jquery.cookie.js', array('jquery'), ICL_SITEPRESS_VERSION);
  1. On the same function, we had added sitewpml on your params:

// Send params to javascript
$params = array(
'ajax_url' => plugins_url('ajax.php', FILE),
'cookie' => $cookie,
'pageLanguage' => defined('ICL_LANGUAGE_CODE')? ICL_LANGUAGE_CODE : get_bloginfo('language'),
'languageUrls' => $language_urls,
'sitewpml' => site_url() . '?wpml_geoip=1',
);

  1. Lastly, on your browser-redirect-geoip.js, your jquery with comment Get the country code to use IP, we replace data: to url :

//Get the country code to use by IP
jQuery.ajax({
type: 'GET',
//data: {'wpml_geoip' : 1},
url: wpml_browser_redirect_params.sitewpml,
async: false,
success: function (ret)
{
browserLanguage = ret.country_code
}
});

Hope this helps

@ryanyagin1902 Please send these code changes as a pull request, and I'd be happy to merge them.

@khromov i created branch on my local copy but I cannot push it on your git