WP.com Geo Uniques

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;

Testing in VIP Quickstart

In simple mode you can override the country by adding a local-config.php file with:



For example, this will force the country to Canada. Feel free to change the value as needed to test different countries.

For advanced mode, you’ll need the following filter somewhere (local-only):

add_filter( 'wpcom_geo_api_endpoint', function() {
    return 'http://vip.local/geo.json';
} );

Add a geo.json file in the public root that mirrors the output of https://public-api.wordpress.com/geo/ and change the values as needed.

Ready to get started?

Tell us about your needs

Let us lead the way. We’ll help you select a top tier development partner. We’ll train your developers, operations, infrastructure, and editorial teams. We’ll coarchitect your deployment processes. We will provide live support for peak events. We’ll help your people avoid dark alleys and blind corners, and reduce wasted cycles.