Free & Easy WordPress Content Geolocation without Plugin!

Displaying custom content based on the country location of a visitor; the best and most accurate geolocation plugin for WordPress is Geotargeting WP (trust me, I have tried them all) — looking for a cheaper solution I discovered an easy way to build a basic system without a plugin; for completely free.

These are simple instructions that anyone with little WordPress understanding and functions.php editing skills can follow — or others can improve on.

WordPress Content Geolocation

Warning: editing your functions.php can break your site; make backups.

What’s the catch?

  1. The location information of users IP addresses is based on free MaxMind geolite2 database — considered the best out of free options but not perfect. Personally for me it has been very adequate and it’s even updated on a monthly basis.
  2. To get the location data; I’m using a third-party GeoIp API which is hosted for free by a kind individual. While there are no limits to how much you can use this service, your IP can get blocked for abuse and you have no control over it.

Good news however are:

  • It is hosted on a DigitalOcean server in the Netherlands (on both HTTP & HTTPS!) which is considered reliable and I’ve never had problems with it.
  • It’s possible to create a clone of the API and run it on your own server (instructions available); which will give you full control.
  • There are other public or self-hosted geolocation APIs that you can also use (this code needs the data in JSON format) such as FreeGeoIP and others.

No worries if you didn’t quite get all that; you can just use the code & be happy.

The idea is simple:

  1. You have a code in your functions.php that creates a shortcode such as “[geolocation]” which defines a country or group of countries and the custom content.
  2. You can place the shortcode “[geolocation]” in post, pages, widgets and when a visitor triggers the country location requirements, it will show the content as desired.

Add this to your functions.php:

Please note; two-letter codes are used to define country names.

function geolocation_function() {
 $ip = $_SERVER["REMOTE_ADDR"];
 $url = "https://geoip.nekudo.com/api/" . $ip;
 $json = file_get_contents($url);
 $data = json_decode($json, TRUE);
 $location = print_r($data['country']['code'], true);
 $country = "US,UK,AU,IN";

if (strpos($country, $location) !== false)
 { return 'Result 1. Visitor country is a match!'; }
 else
 { return 'Result 2. Business as usual.'; }}

add_shortcode('geolocation', 'geolocation_function');
  1. Define a country or a list of countries in “$country”
  2. If location of visitor is found from list of countries — show “result 1”
  3. If location of the visitor is anything else — show “result 2”
  4. Use shortcode [customcontent] in content to trigger the code

You can also include the country name of the visitor in the custom content — for example with the following edit in the code:

 ...
 $location = print_r($data['country']['code'], true);
 $name = print_r($data['country']['name'], true);
 $country = "US,UK,AU,IN";

 if (strpos($country, $location) !== false)
 { return 'Result 1. Visitor country ' . $name . ' is a match!'; }
 else
 { return 'Result 2. Business as usual in ' . $name; }}
 ...

See this post for a better example on showing location data.

The shortcode will work normally on post & pages; but to use it inside html or text widgets add these two lines to your functions.php (before the geolocation code):

add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );

And that should cover the basics — comments welcome.

PS. In this example there is only two content triggers; one for a list of countries and the other for everyone else. You can also include more custom content with a more complex set of triggers — also by city location.

Important stuff!

  • This will never work with caching — you need to disable WP Rocket, W3 Total Cache, WP Super Cache or any other caching method for the page/post using this.
  • If your website is behind a proxy or firewall that is preventing it from seeing the visitors real IP address; try to replace $_SERVER[“REMOTE_ADDR”]; with $_SERVER[“HTTP_X_REAL_IP”]; — to see if that works.

And there you have it without any plugins — custom content shown based on country location. And the free IP Geolocation API also knows things like City name, Full country name, Latitude & longitude, Time zone.

Possibilities are endless really.

Thanks for reading and please, share or mention my post if you benefit from it!

Regards, Tim.

Twitter Button Facebook Button Google Button LinkedIn Button

Leave a Reply

Your email address will not be published. Required fields are marked *