WP.com Geo Uniques

Target distinct markets with different content by geo-targetting users at a country level, while still taking advantage of WordPress.com VIP’s built-in caching benefits.

Usage: Simple Mode

If you just need to know which country the user is visiting from, Simple Mode (which is the default) is all you need. Simply register the countries you want to support and a default (if desired). wpcom_geo_get_user_location() will return the user’s country value via a corresponding two-letter code (e.g. “us”, “ca”, etc.)

wpcom_vip_load_plugin( 'wpcom-geo-uniques' );
// Set default and other supported countries
wpcom_geo_set_default_location( 'us' );
wpcom_geo_add_location( 'ca' );

$country = wpcom_geo_get_user_location();
if ( 'ca' === $country ) {
    echo "Canadian, eh?";
} else {
    echo "USA! USA!"; // 'us' is set to the default

Usage: Advanced Mode

By default, geo-location happens at a country-level but this can be extended to cities and states in advanced mode. Please note that there is an added performance cost to this approach and should be considered after close consultation with the VIP team.

This requires three pieces:

  • enabling advanced mode
  • defining your location groups
  • writing some js that handles group assignment based on the user’s geolocation data

In your theme:

// Explicitly enable advanced mode
add_filter( 'wpcom_geo_simple_mode', '__return_false', 999 );

// Return the region value for use in the cache key
wpcom_geo_add_location( 'san-francisco' );
wpcom_geo_set_default_location( 'national' );

// This is the URL to the js file where we will handle our location assignment
add_filter( 'wpcom_geo_client_js_src', function( $url ) {
    return get_template_directory_uri() . '/js/geo.js';
} );

In the JS file:

wpcom_geo.set_detect_success_callback( function( geo_data ) {
    // Figure out the user's location based on the returned data.
    // geo_data is an object that includes:
    // - latitude
    // - longitude
    // - country_short
    // - country_long
    // - region
    // - city

    if ( 'SAN FRANCISCO' === geo_data.city ) {
        return 'san-francisco';

    return false; // default

} );

wpcom_geo.set_detect_error_callback( function() {
    // TODO: handle error here; default location is already set.
} );

// Kicks things off;


Please get in touch before using this plugin.

This plugin relies on WP.com-specific functions to be fully functional but will work in your local environment with WP_DEBUG === true.

Note that either of these techniques should only be used with a very small list of countries for performance reasons.

If you want to show a specific page to a common group of countries, (for example show a cookie notice on all EU countries) the initial solution might seem to be using

wpcom_geo_add_location( 'fr' );
wpcom_geo_add_location( 'uk' );
... etc

But this will actually create a separate cache bucket for each region. greatly diminishing the cache hit % in European countries.

A better way would be to create 2 separate buckets. One for EU and one for the rest of the world like such:

if ( function_exists( 'vary_cache_on_function' ) ) {
    'if ( isset( $_SERVER["GEOIP_COUNTRY_CODE"] ) && in_array( strtolower( $_SERVER["GEOIP_COUNTRY_CODE"] ), ["be", "bg", "cz", "dk", "de", "ee", "ie", "el", "es", "fr", "gb", "hr", "it", "cy", "lv", "lt", "lu", "hu", "mt", "nl", "at", "pl", "pt", "ro", "si", "sk", "fi", "se", "uk"], true ) ) {
        return true;
    } else {
        return false;