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 === "https://wordpress.stackexchange.com/") {
//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.)