Help implementing PHP script

JimHalpert

Supreme Member
Joined
Jan 16, 2016
Messages
1,345
Reaction score
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)

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;
    }
}
?>
 
At the top of wp-config.php (below <?php line) add:
define( 'WP_DEBUG', true );

Then re-load the page & see if an error message is shown with more information.
 
Hi there, thanks for the reply. I have reloaded the page and it still appears blank. No error messages noted.
 
If you still need help with this, try adding all the code (both parts) in your child-theme functions.php file (at the very end but before "?>" if exist. If it works, then you can move the code to separate files and include them from your functions.php file :)

Your 2 code parts are hooks for Wp, so they must reside wether in a plugin or in your theme functions.php file.
 
Back
Top