WPCS - WordPress Currency Switcher Professional

Set currency by visitor country in WordPress with GeoIP rules

To set the currency by visitor country in WordPress, WPCS – WordPress Currency Switcher uses GeoIP rules: you assign countries to each currency, and a new visitor sees prices in the currency of their country without touching the switcher. The setup takes a few minutes. What surprises most site owners is when the rules apply: on the first visit only, and never against a currency the visitor picked. Below are the setup, the welcome currency, the two geolocation libraries, how to switch the feature off or extend it, and what search engines see.

How the currency is chosen by country

On a visitor's first page view the plugin does four things:

  1. It sets the Welcome currency as the visitor's currency.
  2. It finds the visitor's country from their IP address, with the library selected in Geo position.
  3. It goes through your currencies in order and takes the first one whose GeoIP rule contains that country.
  4. It saves the result in the visitor's Currency storage.

On every following page view the plugin uses the saved currency and does not look at the rules again. A currency the visitor picks in the switcher replaces the saved one and stays. On that very first page view a matching rule also wins over a currency in the link, so a visitor from the United Kingdom who arrives through ?currency=EUR still gets the currency of the United Kingdom rule.

"First visit" means that the storage has no record of the visitor yet. With the default transient storage the record belongs to the IP address and expires one day after the last page view; with cookie it lasts one day from the first visit; with session it ends with the PHP session. When the record expires, the next page view is a first visit again and the rules run once more.

GeoIP only chooses the currency. Prices still need to be printed with [wpcs_price] to be converted; if that is not set up yet, start with how to show prices in multiple currencies in WordPress.

Set up the GeoIP rules

  1. Open Settings → Currency Switcher. On the Currencies tab, add every currency you want to show and make sure each one has a rate (see how to add a new currency).
  2. Open the GeoIP rules tab. Each currency has its own field, Choose a country.... Click it, type a country name and select it. Add as many countries as you need.
  3. Press Save options.

Three rules of thumb:

  • Countries only. There are no entries for regions or continents. To show euros to the whole Eurozone, add each euro country to the EUR rule; to show dollars across the Americas, add each country of the Americas to the USD rule.
  • One rule per country. If a country sits in two rules, the currency higher in the list wins.
  • Leave the rest empty. Countries that are in no rule get the welcome currency, so you do not have to list the whole world.

An example for a site with the base currency USD:

CurrencyCountries in its rule
EURGermany, France, Italy, Spain, the Netherlands and the other euro countries
GBPUnited Kingdom
CADCanada
USDNone: every other country gets the welcome currency, USD

The bottom of the GeoIP rules tab shows the country detected for your own connection, which is a quick check that detection works on your server.

Welcome currency, base currency and one currency for everyone

The two settings are easy to mix up:

  • The base currency is the one with the selected radio button on the Currencies tab. Its rate is 1, and every amount in [wpcs_price] is written in it. It does not decide what visitors see.
  • The welcome currency, set in Welcome currency on the Options tab, is what a first-time visitor sees when no GeoIP rule matches their country. Check it after adding currencies, because it can be any currency of the list, not only the base one.

So a site can keep its prices in INR and still greet visitors in USD. To show GBP to the United Kingdom and USD to everyone else, set Welcome currency to USD and add United Kingdom to the GBP rule; nothing else is needed. To show one currency to everyone, clear all GeoIP rules and set that currency as the welcome currency; if visitors should not change it, do not place a switcher on the site.

A change of the welcome currency or the rules affects new visitors. Visitors the plugin already remembers keep their saved currency until their record expires.

Choose the geolocation library

The Geo position option on the Options tab decides how the country is found:

  • Open Geo IP (default) uses the country database file that ships with the plugin. There is nothing to set up, but the file is only as current as the plugin version, and it handles IPv4 addresses only. If the site is behind Cloudflare with IP Geolocation enabled, it takes the country from Cloudflare's CF-IPCountry header instead.
  • GeoIP2 (MaxMind API key is required) uses MaxMind's GeoLite2 database, which is more current and handles IPv6 too. Create a free GeoLite2 account at MaxMind and generate a license key. Select GeoIP2, paste the key into MaxMind license key, press Save options, then press Update/Download DB. The file is saved as wp-content/uploads/currency-switcher/GeoLite2-Country.mmdb. It is not refreshed automatically, so press the button again every few months. Until the file exists, no country is detected and every visitor gets the welcome currency.

If the detected country is wrong, empty or the same for everyone, the cause is usually the IP address your server passes to PHP. That is covered in GeoIP currency detection not working.

Turn GeoIP off or change how it behaves

Turn it off

Remove all countries from every rule on the GeoIP rules tab and press Save options. Every new visitor then starts with the welcome currency.

Keep the rules, stop the automatic switch

The wpcs_geo_position filter receives the detected country code before the rules are checked. Return an empty value and no rule ever matches, while the rules stay saved in the settings. Do not edit the plugin files for this: changes there are lost with the next update.

/*
 * WPCS: keep the GeoIP rules saved but never switch the currency by country.
 * Child theme functions.php or a code snippets plugin.
 */
add_filter('wpcs_geo_position', '__return_empty_string');

The same filter can supply the country from another source, for example Cloudflare's header when you use GeoIP2; the troubleshooting article above has that snippet.

Apply the rules at every new browser session

Sometimes the country should win at each new visit, not only the first one, while a currency picked in the switcher still holds during the visit. This needs a few lines of code. The snippet below treats each browser session as a first visit: it re-applies the rules, or the welcome currency for countries without a rule, on the first page view after the browser was opened.

/*
 * WPCS: apply the GeoIP rules at the start of every browser session.
 * A currency picked in the switcher still wins until the browser is closed.
 * Child theme functions.php or a code snippets plugin.
 */
add_action('template_redirect', function () {
    global $WPCS;
    if (empty($WPCS) || isset($_COOKIE['wpcs_geo_session'])) {
        return;
    }
    // A cookie without expiry date lives until the browser is closed.
    setcookie('wpcs_geo_session', '1', 0, COOKIEPATH, COOKIE_DOMAIN);

    if (isset($_GET['currency'])) {
        return; // the visitor arrived with a currency in the link
    }

    $country  = (string) $WPCS->storage->get_val('wpcs_user_country');
    $currency = $WPCS->get_currency_by_country($country);
    if (!$currency) {
        $currency = $WPCS->get_welcome_currency();
    }
    $WPCS->storage->set_val('wpcs_current_currency', $currency);
    $WPCS->current_currency = $currency;
});

The snippet runs only when WordPress builds the page. Pages delivered from a page cache skip it, so use it on sites without full-page caching, or exclude the entry pages from the cache.

What search engines see

The plugin has no special handling for crawlers. A search engine bot is a visitor like any other: on its first request it gets the currency of the GeoIP rule for its country, or the welcome currency. Google crawls mostly from addresses in the United States, so if the United States is in a rule, your pages are indexed with that currency; otherwise they are indexed with the welcome currency. Crawlers located in other countries go through the same rules. Every currency is shown under the same URL, so each page is indexed in one currency. To control which one, choose the welcome currency, and the currency of the United States rule, with search results in mind.

Check that it works

Create a draft page with three shortcodes:

[wpcs_check_country]
[wpcs_price value=100]
[wpcs]

[wpcs_check_country] prints the country the library detects for the current visitor, before the wpcs_geo_position filter, and the price shows which currency is active. Each test must be a real first visit: a new private window with cookie or session storage, a different connection with transient storage. A development copy on your own computer, where PHP sees the loopback address, always reports a test address in the United States. If the site uses a page cache, set I am using cache plugin on my site to Yes on the Options tab and clear the cache, otherwise one visitor's page is served to everyone.

  • The rules contain countries, not regions, and each country is in one rule.
  • The welcome currency is the currency for every country without a rule.
  • With GeoIP2, the key is saved and the database file was downloaded.
  • The test was a first visit for the selected Currency storage.

If the test page shows the right country and the currency still does not follow the rules, open a topic on the WPCS support forum with a link to the test page and your Geo position and Currency storage settings.