redirect not working
diretur opened this issue · comments
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
Please replace first few lines file js/browser-redirect-geoip.js with following code. One thing more please deactivate the "WPML Country Detector" plugin as both plugins have same function name. This stops the plugin to work. I have found the issue after spending some hours. Hope this will help.
if(GEOIP_DEBUG)
{
//IE 8/9 fix
if(!window.console)
{
var console = {
log : function(){},
warn : function(){},
error : function(){},
time : function(){},
timeEnd : function(){}
}
}
else // this is the code we added to remove debug error
{
var console = window.console;
}
}
And here is the complete "browser-redirect-geoip.js" JS file code. You can copy paste it.
jQuery(document).ready(function()
{
var GEOIP_DEBUG = true;
if(GEOIP_DEBUG)
{
//IE 8/9 fix
if(!window.console)
{
var console = {
log : function(){},
warn : function(){},
error : function(){},
time : function(){},
timeEnd : function(){}
}
}
else
{
var console = window.console;
}
}
if (jQuery.cookie != undefined)
{
if(GEOIP_DEBUG)
console.log("Initializing geoip script");
// Check if cookie are enabled
jQuery.cookie('wpml_browser_redirect_test', '1');
var cookie_enabled = jQuery.cookie('wpml_browser_redirect_test') == 1;
jQuery.removeCookie('wpml_browser_redirect_test');
//Only attempt redirection of cookies can be placed with jQuery.cookie
if (cookie_enabled)
{
if(GEOIP_DEBUG)
console.log("cookies enabled");
var cookie_params = wpml_browser_redirect_params.cookie;
var pageLanguage = wpml_browser_redirect_params.pageLanguage;
var cookie_name = cookie_params.name;
//Check if we already did a redirect, and if we didn't...
if (!jQuery.cookie(cookie_name))
{
if(GEOIP_DEBUG)
console.log("We have not redirected yet.");
//Get the country code to use by IP
jQuery.ajax({
type: 'GET',
data: {'wpml_geoip' : 1},
async: false,
success: function (ret)
{
browserLanguage = ret.country_code
}
});
// Build cookie options
var cookie_options = {
expires: cookie_params.expiration / 24,
path: cookie_params.path ? cookie_params.path : '/',
domain: cookie_params.domain ? cookie_params.domain : ''
};
// Set the cookie so that the check is made only on the first visit
jQuery.cookie(cookie_name, browserLanguage, cookie_options);
// Compare page language and browser language
if (pageLanguage != browserLanguage)
{
if(GEOIP_DEBUG)
console.log("Page language is not correct for the user");
var redirectUrl;
// First try to find the redirect url from parameters passed to javascript
var languageUrls = wpml_browser_redirect_params.languageUrls;
if (languageUrls[browserLanguage] != undefined)
{
if(GEOIP_DEBUG)
console.log("Found redirection in conditional 1");
redirectUrl = languageUrls[browserLanguage];
}
else if (languageUrls[browserLanguage.substr(0, 2)] != undefined)
{
if(GEOIP_DEBUG)
console.log("Found redirection in conditional 2");
redirectUrl = languageUrls[browserLanguage];
}
else
{
if(GEOIP_DEBUG)
console.log("The user should have been redirected, but we could not find the localized version of the page.");
}
//Finally do the redirect, if this pages language exists
if (redirectUrl != undefined)
{
if(GEOIP_DEBUG)
console.log("Redirecting user!");
window.location = redirectUrl;
}
}
else
{
if(GEOIP_DEBUG)
console.log("User not redirected because he is already on the right language.");
}
}
else
{
if(GEOIP_DEBUG)
console.log("User has already been redirected.");
}
}
}
});
Thanks,
but it is still not working.
Marco Toniolo
Via Medi 8c
6932 Breganzona
Switzerland
phone: +41 79 643 18 36
www.mtb-mag.com | www.bdc-mag.com | www.ridingtroll.com
skype: diretur_marco
On 7 May 2015 at 10:29, hardeep360 notifications@github.com wrote:
And here is the complete "browser-redirect-geoip.js" JS file code. You can
copy paste it.jQuery(document).ready(function()
{
var GEOIP_DEBUG = true;if(GEOIP_DEBUG)
{
//IE 8/9 fix
if(!window.console)
{
var console = {
log : function(){},
warn : function(){},
error : function(){},
time : function(){},
timeEnd : function(){}
}
}
else
{
var console = window.console;
}
}if (jQuery.cookie != undefined)
{
if(GEOIP_DEBUG)
console.log("Initializing geoip script");// Check if cookie are enabled jQuery.cookie('wpml_browser_redirect_test', '1'); var cookie_enabled = jQuery.cookie('wpml_browser_redirect_test') == 1; jQuery.removeCookie('wpml_browser_redirect_test'); //Only attempt redirection of cookies can be placed with jQuery.cookie if (cookie_enabled) { if(GEOIP_DEBUG) console.log("cookies enabled"); var cookie_params = wpml_browser_redirect_params.cookie; var pageLanguage = wpml_browser_redirect_params.pageLanguage; var cookie_name = cookie_params.name; //Check if we already did a redirect, and if we didn't... if (!jQuery.cookie(cookie_name)) { if(GEOIP_DEBUG) console.log("We have not redirected yet."); //Get the country code to use by IP jQuery.ajax({ type: 'GET', data: {'wpml_geoip' : 1}, async: false, success: function (ret) { browserLanguage = ret.country_code } }); // Build cookie options var cookie_options = { expires: cookie_params.expiration / 24, path: cookie_params.path ? cookie_params.path : '/', domain: cookie_params.domain ? cookie_params.domain : '' }; // Set the cookie so that the check is made only on the first visit jQuery.cookie(cookie_name, browserLanguage, cookie_options); // Compare page language and browser language if (pageLanguage != browserLanguage) { if(GEOIP_DEBUG) console.log("Page language is not correct for the user"); var redirectUrl; // First try to find the redirect url from parameters passed to javascript var languageUrls = wpml_browser_redirect_params.languageUrls; if (languageUrls[browserLanguage] != undefined) { if(GEOIP_DEBUG) console.log("Found redirection in conditional 1"); redirectUrl = languageUrls[browserLanguage]; } else if (languageUrls[browserLanguage.substr(0, 2)] != undefined) { if(GEOIP_DEBUG) console.log("Found redirection in conditional 2"); redirectUrl = languageUrls[browserLanguage]; } else { if(GEOIP_DEBUG) console.log("The user should have been redirected, but we could not find the localized version of the page."); } //Finally do the redirect, if this pages language exists if (redirectUrl != undefined) { if(GEOIP_DEBUG) console.log("Redirecting user!"); window.location = redirectUrl; } } else { if(GEOIP_DEBUG) console.log("User not redirected because he is already on the right language."); } } else { if(GEOIP_DEBUG) console.log("User has already been redirected."); } }
}
});
—
Reply to this email directly or view it on GitHub
#5 (comment)
.
Hi,
What is the problem? Can you please tell me about the error it is showing?
Thanks
Hardeep
solved, thanks
Two questions:
- is there a way to redirect before the user sees the default language?
- My third language does not get redirected
thanks
Hey @diretur
1.) No. The redirect is implemented in JavaScript via AJAX. This is to to bypass any full-page caching plugin that may be in use. Thanks to this though, you can use a caching plugin. :-)
2.) Please make sure you have the language_mappings correct. WPML code can be anything you set when you configure the language while MaxMind codes are a strict standard, see:
https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/blob/master/WPML_GeoIP_IPResolver.class.php#L18
For debugging, please enable GEOIP_DEBUG constant. It will show you full debug logging in the browser console, like why you were or were not redirected:
https://github.com/khromov/wp-wpml-geoip-browser-language-redirect/blob/master/js/browser-redirect-geoip.js#L3
Hey Stanislav,
this is my mapping, but the Spanish language is not detected:
$this->language_mappings = array(
'IT' => 'it', //Italy
'ES' => 'es', //Spain
'CO' => 'es', //Colombia
'CL' => 'es', //Chile
'MX' => 'es', //Mexico
'BO' => 'es', //Bolivia
'CR' => 'es', //Costa Rica
'PE' => 'es', //Peru
'AR' => 'es' //Argentina
);
Thanks
Marco
Marco Toniolo
Via Medi 8c
6932 Breganzona
Switzerland
phone: +41 79 643 18 36
www.mtb-mag.com | www.bdc-mag.com | www.ridingtroll.com
skype: diretur_marco
On 20 May 2015 at 18:47, Stanislav Khromov notifications@github.com wrote:
Hey @diretur https://github.com/diretur
1.) No. The redirect is implemented in JavaScript via AJAX. This is to to
bypass any full-page caching plugin that may be in use. Thanks to th is
though, you can use a caching plugin. :-)2.) Please make sure you have the language_mappings correct. WPML code can
be anything you set when you configure the language while MaxMind codes are
a strict standard, see:For debugging, please enable GEOIP_DEBUG constant. It will show you full
debug logging in the browser console, like why you were or were not
redirected:—
Reply to this email directly or view it on GitHub
#5 (comment)
.
You will have to enable errors in the javascript like I explained and send a full debug log. Otherwise it's impossible to know what's wrong. This plugin is for developers, if you can't do debugging you should ask someone who does. :-)
right, now it's on http://www.mtb-mag.com/
Marco Toniolo
Via Medi 8c
6932 Breganzona
Switzerland
phone: +41 79 643 18 36
www.mtb-mag.com | www.bdc-mag.com | www.ridingtroll.com
skype: diretur_marco
On 21 May 2015 at 08:50, Stanislav Khromov notifications@github.com wrote:
@diretur https://github.com/diretur
You will have to enable errors in the javascript like I explained and send
a full debug log. Otherwise it's impossible to know what's wrong. This
plugin is for developers, if you can't do debugging you should ask someone
who does. :-)—
Reply to this email directly or view it on GitHub
#5 (comment)
.
Works fine for me. When I go to that url I get redirected to the english version.
From the log:
Initializing geoip script
cookies enabled
We have not redirected yet.
Page language is not correct for the user
Found redirection in conditional 1
Redirecting user!
Navigated to http://www.mtb-mag.com/en/
I assume your default_language is "en", because it is not in your mapping array.
I can't see anything being wrong here, so I am unable to help you debug any further. May I suggest hiring a developer or use the built-in WPML feature. Closing.
It's the Spanish language that's not working, as I explained above.
Never mind, I will ask another developer
On Thursday, May 21, 2015, Stanislav Khromov notifications@github.com
wrote:
@diretur https://github.com/diretur
Works fine for me. When I go to that url I get redirected to the english
version.From the log:
Initializing geoip script
cookies enabled
We have not redirected yet.
Page language is not correct for the user
Found redirection in conditional 1
Redirecting user!
Navigated to http://www.mtb-mag.com/en/I assume your default_language is "en", because it is not in your mapping
array.I can't see anything being wrong here, so I am unable to help you debug
any further. May I suggest hiring a developer or use the built-in WPML
feature. Closing.—
Reply to this email directly or view it on GitHub
#5 (comment)
.
Marco Toniolo
+41796431836
MTB-MAG.com | BDC-MAG.com
@diretur I understand what you are describing, but surely you understand that since I am not located in Spain I can't debug that specific problem for you. A developer with access to the PHP files will be able to help you better.