Detecting the Country Code of your user with SiteGround and CloudFlare Caching Enabled using WordPress

Michael Allen Smith
3 min readApr 8, 2018

I have caching enabled for my WordPress websites using both the SuperCacher offered by my web host SiteGround and my CDN CloudFlare. This enables my websites to run smoking fast.

My site INeedCoffee.com has an Amazon Ad below the content of each article. The ads pay for the site. The problem is that until recently, the only ad displayed on the website was for Amazon USA. That doesn’t help my readers outside of the USA. 15% of my audience lives in either Canada or the UK. They should not see an ad to Amazon USA. They would be best served with an ad to Amazon Canada or Amazon UK or no ad at all.

Our goal is to detect where the user is coming from and deliver the correct ad and do it in a way that still allows the website to be cached for fast page loads.

#1 CloudFlare

From the CloudFlare dashboard go to Network and scroll down to IP Geolocation. Confirm that this is turned on.

Look for this setting under Network.

#2 Detect Country Code with PHP

Create a new PHP file called lookup.php (or whatever you like) and add the following line.

// SETUP: From the CloudFlare dashboard go to Network
// and scroll down to IP Geolocation. Confirm that this is turned on.
$country_code = strtolower($_SERVER[“HTTP_CF_IPCOUNTRY”]);

Place this file in the root directory of your WordPress blog installation.

#3 Disable Caching for Lookup Code

This is the most important step. Without this action, the lookup result will be cached and users will be incorrectly attributed to countries of previous users. You want to tell both SiteGround and CloudFlare to not cache the lookup code.

From the CloudFlare dashboard, go to Page Rules. Add a Page Rule for the URL: https://your-domain-here.com/lookup.php. Set Cache Level to Bypass.

On SiteGround, from the SG Optimizer plugin, go to SuperCacher Config. Add https://your-domain-her.com/lookup.php to “Exclude URLs From Dynamic Caching” and hit the Update button.

Now your lookup code will not be cached.

#4 Install the Ad Inserter Plugin

Actually, you can use any Plugin that allows you to embed PHP code. I have used both Ad Inserter and PHP Code Widget.

If you use the Ad Inserter plugin, be sure to press the PHP button in the upper right. There is a little bit of a learning curve with that plugin, so get it working first with basic HTML before moving onto the next step.

Hit the php button if you use the Ad Inserter Plugin.

The PHP Code Widget is straight-forward but doesn’t have pretty syntax highlighting.

#5 Write some PHP

Below is some sample code that uses the Country Code from lookup.

In this example, I am able to reach out directly to those users visiting from Canada or the UK. You can add as many countries as you like. I assume the default user is coming from the USA.

Last Words

I hope this code helps you. You will need a VPN that allows you to set your country of origin to test.

Disclosure: I am a proud affiliate for SiteGround.

** Thanks for reading and any claps!

** You can find my non-tech posts on my blog CriticalMAS.org. I also write for my coffee website INeedCoffee.com. Check them out if you are interested.

--

--