After googling around and reading, I found the code below. Problem is, it’s been a few weeks and I’ve lost the original post.
I have now done my best in understanding how to create a function hooked into template_redirect so that I can re-direct visitors based on the country they are from.
I understand that this is not bullet-proof and what not, but it’s the best working solution I found that does not require a paid plugin.
This is the IP and Country function:
function visitor_ip_and_country() {
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
$country = "Unknown";
if ( filter_var( $client, FILTER_VALIDATE_IP ) ) {
$ip = $client;
}
elseif ( filter_var( $forward, FILTER_VALIDATE_IP ) ) {
$ip = $forward;
} else {
$ip = $remote;
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://www.geoplugin.net/json.gp?ip=".$ip);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
$ip_data_in = curl_exec( $ch )
curl_close( $ch );
$ip_data = json_decode( $ip_data_in, true );
$ip_data = str_replace( '"', '"', $ip_data );
if ( $ip_data && $ip_data['geoplugin_countryName'] != null ) {
$country = $ip_data['geoplugin_countryName'];
}
return 'IP-address: '.$ip.' | Country: '.$country;
}
I have then tried to create a function that re-directs users, but I keep getting an “undefined variable” error.
Here is that code:
add_action( 'template_redirect', 'redirect_canadians' );
function redirect_canadians() {
$redirect = '';
$vc = visitor_ip_and_country($country);
if ($vc == 'Canada') {
$redirect = 'https://example.com';
wp_redirect($redirect);
die();
}
}
It refers to this line:
$vc = visitor_ip_and_country($country);
I don’t know how to get the re-direct function to check the argument from the visitor_ip_and_country() function.
I need help.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The problem is the variable $sountry – and this is because of something called variable scope – https://www.php.net/manual/en/language.variables.scope.php – the variable is only available within the function, as you do not “export” it or make it globally available – here is an update to your functions to show how this might work:
function visitor_ip_and_country() {
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
$country = "Unknown";
if ( filter_var( $client, FILTER_VALIDATE_IP ) ) {
$ip = $client;
}
elseif ( filter_var( $forward, FILTER_VALIDATE_IP ) ) {
$ip = $forward;
} else {
$ip = $remote;
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://www.geoplugin.net/json.gp?ip=".$ip);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
$ip_data_in = curl_exec( $ch )
curl_close( $ch );
$ip_data = json_decode( $ip_data_in, true );
$ip_data = str_replace( '"', '"', $ip_data );
if ( $ip_data && $ip_data['geoplugin_countryName'] != null ) {
$country = $ip_data['geoplugin_countryName'];
}
return array(
'ip' => $ip,
'country' = >$country
// note, you could add additional keys to this array
);
}
add_action( 'template_redirect', 'redirect_canadians' );
function redirect_canadians() {
// call your function to get an array with IP and country values ##
$array = visitor_ip_and_country();
$redirect = '';
$vc = $array['country'];
if ($vc == 'Canada') {
$redirect = 'https://example.com';
wp_redirect($redirect);
die();
}
}
This is a simple solution, get it working, then refine it more.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0