March 6th, 2019 - Code Snippets, Quick Tips, WordPress

Redirect Users Based on Country using WP-Engine’s GeoTargeting

One of the most popular WordPress specialized web hosts is WP Engine. They offer some of the best support around, and some fantastic tools.

An interesting feature they added recently comes in the form of “GeoTargeting“. This will allow you to provide unique content to your visitor based on their location. Very useful for larger companies with multiple locations or an array of international customers.

By default, this feature is disabled on accounts. However, just contact support and they can get this turned on for you. Once it is enabled on your account you will need to install and activate the free WP Engine GeoTarget Plugin. You can read the full article here about Enabling GeoTargeting at WP Engine.

Out of the box you are provided with a series of shortcodes which you can use to customize content on your site based on the users location. This can be useful for single websites, but what if you have multiple sites such as in a WordPress Multisite Network?

Scenario: Let’s say you are using Multisite and you have a network of sub-sites for different countries or continents. You could add some links in the header of your site to instruct users to click different flags for their country, but why not automate this process?

Solution: We are going to toss together a basic WordPress plugin with some simple PHP code to help redirect users to different sub-sites depending on location.

Note: this tutorial will require basic knowledge of PHP, FTP, and the overall structure and functionality of WordPress.


Once you have GeoTargeting enabled on your account and the WP Engine plugin installed we need to create a new custom plugin. You will want to FTP into your site and navigation to your /wp-content/plugins/ folder. Here you will want to create a new folder. You can name this anything you’d like, we called ours “georedirect”. Once created, open this folder and create a new file called: “georedirect.php”.

Copy and paste the below code into this new PHP file:

/*
Plugin Name: WP Engine GeoTarget Redirect
Description: Redirecting visitor traffic based on geo-location using the WPEngine GeoTarget feature
Author: Zealth Digital Marketing
Version: 1.0
License: GPLv2
*/

function wpe_country_redirect() {

$country = null;

// Check to see if WP Engine GeoTarget plugin is installed
if ( class_exists( 'WPEngine\GeoIp' ) ) {
$geo = WPEngine\GeoIp::instance();
$country = $geo->country();
}

// Create redirect rules based on country codes
switch($country){

// country codes for Europe
case 'AL': case 'AD': case 'AT': case 'BY': case 'BE': case 'BA': case 'BG': case 'HR': case 'CZ': case 'DK': case 'EE': case 'FI': case 'FR': case 'DE': case 'GR': case 'HU':
wp_redirect( 'http://website.com/eu/', 301 );
exit;

// country codes for India
case 'IN': case 'BD': case 'NP': case 'TH': case 'KH': case 'VN': case 'MY': case 'SG': case 'ID': case 'PH': case 'AU': case 'PK': case 'AF': case 'MM': case 'BN': case 'TL':
wp_redirect( 'http://website.com/in/', 301 );
exit;

default:
break;
}
}
add_action( 'init', 'wpe_country_redirect' );

We have added comments to this code to help explain what is going on. In this example we have 2 sub-sites in our network, one for our Europe customers, and another for customers based in India. You will see we defined a list of country codes followed by a redirection rule to the URL for the specific sub-site. You can add or remove as many country codes as you’d like as well as create additional rules for additional sub-sites following the same format.

Once you are done, simply login to the backend of the main site, and activate the plugin. You can use a VPN service to test this as well as several GeoTargeting Testing tools by searching Google.