0

Just wanted some advice in relation to adding some custom code to my wordpress site. I am using geoip to check a users country code and redirect them to the relevant site when they land on my homepage.

Currently I include this block of php code at the very top of my themes header.php file. However I'm not sure that is the best way to go about it, would it be better off in the functions.php and if so is there a hook I can use to call this code when the homepage is about to be loaded and redirect accordingly?

$url = $_SERVER['REQUEST_URI'];
require_once("geoip.inc");
$gi = geoip_open(dirname(__FILE__) . "/GeoIP.dat", GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
if ($url === "/") {
    //If IE go to /ie
    if ($country_code === "IE") {
        header("Location: http://www.wpmultisite.com/ie");
        die();
    }//If GB go to /uk
    else if ($country_code === "GB") {
        header("Location: http://www.wpmultisite.com/uk");
        die();
    }
}

I should point out I'm trying to set up a network of sites using the Wordpress Multisite functionality.

Many thanks in advance for any of your help.

3 Answers 3

1

I'd recommend moving this to a plugin, rather than doing it in the theme, and then activating it on the main site. The rule that I follow is that if it's going to affect the look, it goes in the theme; if it's affecting functionality, then it goes in a plugin. This, to my mind, is a functional thing.

wp-content/plugins/my-ms-redirector/my-ms-redirector.php:

<?php

/*
Plugin Name: My MS Redirector
Description: Redirects to /ie, /uk, etc based on geoip
*/

add_action( 'init', 'wpse206906_redirector' );

function wpse206906_redirector() {
    $url = $_SERVER['REQUEST_URI'];
    require_once("geoip.inc");
    $gi = geoip_open(dirname(__FILE__) . "/GeoIP.dat", GEOIP_STANDARD);
    $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
    geoip_close($gi);
    if ($url === "/") {
        //If IE go to /ie
        if ($country_code === "IE") {
            wp_redirect( 'http://www.wpmultisite.com/ie' ) ;
            exit();
        }//If GB go to /uk
        if ($country_code === "GB") {
            wp_redirect( 'http://www.wpmultisite.com/uk' );
            exit();
        }
    }
}

References

(I know that die() and exit() are functionally identical, but I tend to use exit() when I just want to end PHP execution, and die() when I want to throw an error. It's a personal preference.)

1

I believe you can use this code as early as you can. I personally would give this task to the webserver and/or Varnish or similar great pieces of software.

In case of WordPress, you could either prepend /index.php with this, or attach to one of the most early hooks there are, like muplugins_loaded, plugins_loaded, setup_theme and so on.

1

Try the following in your functions.php file

add_action('wp_head','language_redirect');

function language_redirect() {


require_once("geoip.inc");
$gi = geoip_open(dirname(__FILE__) . "/GeoIP.dat", GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
if (is_front_page()) {
    //If IE go to /ie
    if ($country_code === "IE") {
        header("Location: http://www.wpmultisite.com/ie");
        die();
    }//If GB go to /uk
    else if ($country_code === "GB") {
        header("Location: http://www.wpmultisite.com/uk");
        die();
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.