One of the best things about having an online business is that you can reach customers all over the world. Of course this creates unique complications, as users in different countries and regions have specific needs, and are subject to different laws. For eCommerce sites, knowledge of your site’s visitors is especially important for calculating tax rates. In addition, it’s also important to consider if you are selling something that could be illegal in certain regions.
For this, geolocation awareness — i.e., the ability to programmatically be aware of a visitor’s location, by country, region, or even longitude and latitude — can play a huge role. It is most commonly done using the GeoIP database by Maxmind, a free database used to translate an IP address into location data.
There are several WordPress plugins that incorporate this database to do interesting things. For example, there’s a plugin to automatically switch to a translated version of the site if one is available via WPML, based on a user’s region. There is even a WooCommerce extension for setting different prices based on the visitor’s location.
GeoIP is a very useful technology for all sorts of applications. Calculating sales tax based on location has become more important due to changes in VAT laws in the EU. Other applications include showing location-specific ads, and blocking sales of certain products in specific markets.
Location aware WordPress development with GeoIP detect
Update: I originally linked to the WordPress.org page for GeoIP detect, but it has been removed. The link has been updated to the plugin’s GitHub repository.
GeoIP Detect is a free plugin which auto-updates the GeoIP database and gives you a few useful functions to get location-specific information about users who are making a request to your server, based on their IP address.
Once installed, it will prompt you to install the latest GeoIP database. Once that is done, you will be taken to its settings screen.
On this screen, you can manually update the GeoIP data — though the plugin will auto-update once a month. You can also choose to add a country code class on the body tag. If your theme uses the function body_class() as it should, then you can use this tag to create special CSS rules based on the user’s location.
This plugin gives you everything you need to build custom, geolocation-aware content into your site. For example, if you wanted to filter the title of a post relating to what we call ‘Soccer’ in America, based on location, you could do something like this:
add_filter( 'the_title', function( $title ) { $geoip = geoip_detect2_get_info_from_current_ip(); $country = $geoip->raw[ 'country' ][ 'iso_code' ]; if ( 'US' !== $country ) { $title = str_replace( 'soccer', 'football', $country ); } return $title; });
In using the function geoip_detect2_get_info_from_current_ip() which is provided by this plugin, we get the 2 digit ISO country code for the user’s country. If that code isn’t in the U.S., then we replace the word “soccer” with “football.”
That is an over-simplified example, but I hope you are starting to understand how you can use this sort of information about your users to generate dynamic content. For example, a similar test could be used to replace a checkout/add to cart button with the notice that a product is not for sale in a specific country, or a specific state or province of that country.
The property found in the object that function returns contains an array of really useful information. This next code snippet shows how to get the current visitor’s longitude, latitude, and timezone into variables. From there, you can use this data to show the stores which are closest to them, with store hours in local time. Or, you can use it to direct visitors to the most appropriate event.
$location_data = geoip_detect2_get_info_from_current_ip( )->raw; $location = $location_data[ 'location' ]; $longitude = $location[ 'longitude' ]; $latitude = $location[ 'latitude' ]; $timezone = $location[ 'time_zone' ];
Caching & GeoIP
One of the big catches with GeoIP is that it does not play nicely with a traditional full page caching system. Normally these types of systems store a rendered HTML file of each page on your site. That way they only have to build the page running all of the PHP code and database queries it involves. Of course, if you have one version of your site you show to all users, then any sort of dynamic content, such as geolocation-aware content, isn’t going to work.
One solution for this is to employ fragment caching. This technique, also known as partial page caching, allows you to cache parts of a page, or even just database queries, while dynamically generating other content on each page load. This technique is often used on membership sites that have a small amount of user-specific content to show.
Fragment caching can be useful when working with geolocation-aware content, as you can cache the non-dynamic content using fragment caching, while still dynamically generating geolocation-aware content.
I would also recommend caching the results of the lookup to the GeoIP database in short-lived transients. Here is a simple function to get geolocation data for the site visitor’s IP address, with caching of the data when possible:
function slug_geoip_data() { $ip = geoip_detect2_get_client_ip(); $cache_key = 'geoip_cache' . $ip; if ( false == ( $location = get_transient( $cache_key ) ) ) { $location_data = geoip_detect2_get_info_from_ip( $ip ); if ( is_object( $location_data ) ) { $location = $location->raw; $location = $location_data[ 'location' ]; set_transient( $cache_key, $location, 99 ); } } return $location; }
There are also several caching plugins that can use a different cache for location-specific versions of sites based on GeoIP. This article on WPtest provides a great rundown of the options available for GeoIP compatible caching. As GeoIP becomes more prominent (as I suspect it will), more caching plugins will build in support for GeoIP.
GeoIP is new and exciting — go ahead and make something cool
GeoIP in WordPress is relatively new, but it has a lot of potential. I didn’t just introduce you to the GeoIP Detect plugin so you could use it in your site development, but rather with the hope that you will take a peek at how it uses the PHP API for GeoIP and be inspired to put it to work in something new and unique.
11 Comments
Join the conversation