JimHalpert
Supreme Member
- Jan 16, 2016
- 1,345
- 292
Hi everyone,
I am currently using this wordpress plugin called GeoIP Detection (link) and it had a feature that allows redirection based on country. Code is attached below. My intention is to have a custom PHP script for each page of my site so that each page directs customers to different links. What I have done is as follows:
1) Create a custompage.php script as shown below.
2) This script would reside in my public_html/wp-content/themes/directory2-child
3) Create a new page in wordpress using the above script
However, the redirecting does not seem to work. I get a blank page when I load up the page. Could someone help me out please?
Original code (link)
custompage.php
I am currently using this wordpress plugin called GeoIP Detection (link) and it had a feature that allows redirection based on country. Code is attached below. My intention is to have a custom PHP script for each page of my site so that each page directs customers to different links. What I have done is as follows:
1) Create a custompage.php script as shown below.
2) This script would reside in my public_html/wp-content/themes/directory2-child
3) Create a new page in wordpress using the above script
However, the redirecting does not seem to work. I get a blank page when I load up the page. Could someone help me out please?
Original code (link)
Code:
add_action('template_redirect', 'geoip_redirect', 5);
function geoip_redirect(){
if (is_admin())
return;
// This condition prevents a redirect loop:
// Redirect only if the home page is called. Change this condition to the specific page or URL you need.
if (!is_home())
return;
if (!function_exists('geoip_detect2_get_info_from_current_ip'))
return;
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->country->isoCode;
switch ($countryCode) {
case 'DE':
$url = '/germany';
break;
case 'US':
$url = '/usa';
break;
default:
$url = '';
}
if ($url) {
wp_redirect(get_site_url(null, $url));
exit;
}
}
custompage.php
Code:
<?php /* Template Name: GeoIP Detection */ ?>
<?php
add_action('template_redirect', 'geoip_redirect', 5);
function geoip_redirect(){
if (is_admin())
return;
// This condition prevents a redirect loop:
// Redirect only if the home page is called. Change this condition to the specific page or URL you need.
if (!is_home())
return;
if (!function_exists('geoip_detect2_get_info_from_current_ip'))
return;
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->country->isoCode;
switch ($countryCode) {
case 'DE':
$url = '/germany';
break;
case 'US':
$url = '/usa';
break;
case 'SG':
$url = 'www.google.com';
break;
default:
$url = 'www.google.com';
}
if ($url) {
wp_redirect(get_site_url(null, $url));
exit;
}
}
?>