Website DIY - tricks and solutions

Caching Plugins and Country GeoIP

Although this article was written for users of the CCA plugin; it applies to other plugins that modify content and pages based on the country of your visitor.

If you don’t know what a caching plugin is, then you probably aren’t using one and can ignore this page.

If you only wish to use the CCA plugin to display different content for different categories then you can ignore this page.

If you wish to cache and use visitor country location: then I recommend you use either Comet Cache or WP Super Cache plugin, with the country caching extension.

In this section:

1. Why caching plugins don’t work well with dynamic content
2. Solutions and work rounds
    2a. Comet Cache <(recommended)
    2b. WP Super Cache <(recommended)
    2c. Cloudflare and other CDN’s <(recommended-ish)
    2d. Other Caching Plugins
    2e. Alternative API solution for use with caching
3. If all else fails
4. Testing caching and country location (beware pitfalls)

1. Why caching plugins don’t work well with dynamic content

Before WordPress delivers a page to the visitor a lot of scripts are run to build the page; this may take micro seconds or seconds. Caching plugins save time by by-passing the building of the page, and instead serve a snapshot (cache) of the page as it was created for the “first” vistor.

This is okay for static pages but not for dynamic content e.g. if you are using the WordPress RSS widget and your page was snapshot (cached) 3 days ago, your news feed has in effect become a 3 day old history feed.

Caching is a problem for Country geoLocation plugins which serve “dynamic content”. e.g. A widget may be configured to display an Amazon.DE advert to German visitors, and one for Amazon.COM to all other visitors. However; if the cache was created when the page was visited by someone from Germany then the snapshot will contain the Amazon.DE advert and ALL subsequent visitors will see this German version of the page.

N.B. Using the CCA widget for categories only is NOT a problem. e.g. If your widget is set to display a hotel search form on posts in category Travel (visitor from default “anywhere” only); then the hotel form will be included whenever WordPress has to build the page, and consequently in any cache snapshot.

2. Solutions and work rounds

2a. Comet Cache

Comet Cache is designed to enable developers to add extensions to modify caching behavior. The free Country Caching extension plugin makes CC/ZC cache by page + visitor country instead of just page. With this extension Comet should work perfectly with country geo-location.

More info: Country Caching extension for Comet Cache (the guide).
Get the plugin via your WP dashboard or download here on WordPress.Org

2b. WP Super Cache

Install the Country Caching Extension for WPSC.

Super Cache is also designed to enable coders to add extensions to modify caching behaviour. The free WPSC Country Caching extension plugin makes WPSC cache by page + visitor country instead of just page. With this extension WPSC should work perfectly with country geo-location.

More info: Country Caching extension for WPSC (the guide).
Get the plugin via WP dashboard or download here on WordPress.Org

2c. Cloudflare and other CDN’s

I use Cloudflare with its default caching mode to caches static content like images and CSS. This works fine with the CCA plugin. I do not recommend use of other caching options.

Other CDN’s have similar facilities and their equivalent of Cloudflares static content caching should work well with the CCA plugin.

2d. Other caching plugins

The other major caching plugin is W3 Total Cache. It does not currently enable the use of extensions so it is impossible to produce a cache by page+country add-on like I’ve done for WPSC and Comet Cache (above).

If you are a confident coder, you can modify W3TC to cache by page+country, but your code modification would be lost every time you update to a new version of W3TC. I will do a “how to” on this in a later article.

Most of the popular caching plugins allow you to specify pages/posts that must not be cached. This is fine if you are only serving a couple of posts that serve different customized content depending on visitors country. It may also be fine if your sidebar widgets (CCA or others) only serve different country related content for posts in a category with a small number of posts.

However, if your sidebar widget content is visitor related throughout your site then you would have to disable caching on most pages – making the use of a caching plugin pointless.

Some plugins also allow “fragment caching” which enables you to identify snippets of code that should not be cached. These might work fine on simple code snippets like “echo date(‘Y-m-d H:i:s’)” but I suspect you may get unexpected results if you try “fragment caching” on code that uses variables or that requires say class instances such as a sidebar widget. If anyone has an example of a sidebar widget that has been fragment cached I’d love to see the code.

Comet Cache and WP Super Cache allow you to write extensions to modify their caching behavior; and this is the feature my country caching extensions (above) use. You may find other plugins offer similar hooks for extensions (please let me know) and you can write your equivalent.

2e. CCA’s alternative javascript/Ajax API solution

Client (browser) side geolocation should work with all caching plugins. The downside is that API’s are not as quick as server side geolocation; and won’t work with your shortcodes or widgets. However using the API you can create your own javascript equivalents.

If you are a coder or plugin author you can use the geolocation API provided by the CCA plugin which has advantages over 3rd party APIs. The CCA API returns the visitor’s country code, country name, and an EU/EEA country indicator (useful for EU laws).

If all else fails (CCA plugin only)

Just use the plugin for categories i.e. always set the Country selector of your entries to the “any/other” default.

Caching and country location testing (beware pitfalls)

Pitfall: many caching plugins will serve you a virgin UNcached page if you are logged in as admin (or ANY user). Some will serve you an UNcached page if you have PREVIOUSLY been logged on. If testing, make sure you are NOT logged in and have deleted all cookies. As I have never used it to log in to my site, I use the “360 Browser” for testing,

If you are using the WPSC or Comet Cache extension there is a simple way to check if country caching is working for WPSC or for Comet Cache.

The simplest way to see what your pages look like to visitors from different countries is to go to Locabrowser.com enter the page address and select a country.

Other methods:

I’m based in England so I see my site the same as any other British visitor. To see how my site’s pages appear to a US visitor I can use proxyUSA.Org and enter my site URL’s there. If you use this proxy for testing make sure you untick the “remove scripts” option as most WP sites need javascript to display correctly.

If you are based in the US then Google search for proxy servers that do the same job for other countries. Warning: not all of these actually use IP addresses for the country claimed – I used a so called Australian proxy that actually had a US IP address – if you get unexpected results check the IP address used by the proxy.

If you cant find a suitable proxy some online site speed test sites allow you to specify servers in different countries and will display small snapshots of pages generated.

If you have a “TOR browser” and know what you are doing you can use it to see what your site looks like in a variety of countries.

As at Feb 18 Opera VPN is broken for testing. Whatever VPN locale you select, its IP is identified as either unknown or “EU” by Cloudflare, Maxmind etc. However – this may be fixed in later Opera versions. The simplest way is to test with the Opera Browser which comes with a free VPN. You can select a variety of countries and you will see the content that your server provides to visitors from that country.


  1. Pieter

    How does the Country Caching Extension work with WP Rocket (also one of the more well-known caching plugins nowadays)?

    • AW

      Currently there are extensions for WPSC and Comet cache. “WP Rocket” asked me to produce an extension back in 2015; but at that time Rocket did not have suitable hooks.

      Recently, I checked the latest Rocket code and now believe I could develop and publish a Country Caching Extension for WP Rocket in a matter of weeks that will work with Cloudflare, built in Maxmind or “any” other geolocation system.

      I emailed my old “contact” at WP Rocket 3 months ago (see below) but have not had a response. (I assume email is still valid as according to Linkedin my contact still works there.) If you, as a customer, ask WP Rocket to contact me ( https://webstuff.inblighty.com/contact-us/ ) then they may actually respond, and the plugin can be developed.

      email to WP Rocket:

      Hi L***,

      3 years ago you contacted me (email below) about creating an extension for WP Rocket similar to the Country Caching Extensions for WPSC and Comet. At that time I said I didn’t think it was possible without changes to WP Rocket code.

      I have had numerous emails about this possibility from Rocket users, and after a recent enquiry I checked your latest code on Github; I now think it is eminently doable.

      As I don’t currently have SSH/composer access on my servers I can’t install WP Rocket from Github source – and it is impossible for me to develop and test a WP Rocket “extension” blind.

      If you are happy to provide me with a WordPress installable copy of WP Rocket then I am confident I will be able to develop a flexible Country Caching “extension” for WP Rocket. I can add it to the WordPress Directory (and ideally have its 3rd party helper script included with WP Rocket); or if you would rather provide it as a premium “extension” I would be happy to discuss.

    • Pieter

      Hi Andy,

      Great stuff, I sent them an email already and will keep a close eye on the follow-up and hopefully follow through.


    • Pieter

      You guys should be talking with each other now 🙂
      Hope the cooperation will be fruitful and we can soon use your extension with WP Rocket too!

    • AW

      WP Rocket have contacted me and I now have a copy to test the extension against (when developed). Thanks
      Country Caching for WP Rocket is now available for testing.

Leave a Reply

Your comment will appear after its approved; usually within 12 hours but can be up to a week.
Email is optional and never published. It will only be used to contact you if clarification of your comment is needed.

Copyright © 2014-2024 Webstuff.Inblighty.Com
This site recommends and is hosted by: Kualo Web Hosting.    
Theme: hemingway

Blog home  |  ↑ Top of Page ↑