Show a different currency on different pages in WordPress
A different currency on different pages in WordPress is a common need: a UK landing page that shows pounds, a eurozone offer in euros, while the rest of the site follows the currency each visitor picked in the switcher. WPCS has no per-page currency setting, but it offers three ways to get there, depending on whether one price, the first impression or the whole page must be in a given currency. Below are all three, how to hide the switcher where it makes no sense, and the one limit that stays: amounts are still written in the base currency.
Choose the method
| You need | Use | The visitor's own currency |
|---|---|---|
| One or a few prices always in one currency | The fix_currency attribute | Not changed |
| A page that opens in a currency, but visitors may switch | A link with ?currency=GBP | Becomes GBP for the visit |
| A whole page always in one currency | A short code snippet | Not changed |
One price in a set currency: fix_currency
The [wpcs_price] shortcode accepts a currency code in fix_currency:
[wpcs_price value=100 fix_currency=EUR]
The amount (100 in the base currency) is converted into euros and shown in euros, whatever the visitor selected. Right after printing the price, the plugin switches back to the visitor's currency, so the rest of the page is unaffected. A code that is not on the Currencies tab is ignored. Several of these side by side show one price in several currencies at once.
Three limits:
- When I am using cache plugin on my site is set to Yes on the Options tab, every price is redrawn with AJAX after the page loads, and the redraw does not know about
fix_currency: the price ends up in the visitor's currency. - Do not combine
fix_currencywithtype=fixed, and do not use it on prices that can be empty (an unfilled custom field). In these two cases the shortcode returns before it restores the visitor's currency, and the visitor stays switched to thefix_currencycurrency for the rest of the visit. - It sets the currency of a price, not of the page: other prices and the switcher keep following the visitor.
A link that opens the page in a currency
Any page address with ?currency= and a currency code switches the visitor to that currency: https://example.com/uk-offer/?currency=GBP. The plugin remembers the choice for the following pages, and the visitor can still change it in the switcher. This is the simplest way to send each market's ads or newsletters to the same page in its own currency; the FAQ on currency links without a shortcode shows more ways to build such links.
One detail: on a new visitor's very first page view, a GeoIP rule that matches their country is applied after the link and takes precedence. If your rules and your landing pages disagree, see set currency by visitor country.
Force a different currency on a whole page
For pages that must always show one currency, set the plugin's current currency for that page in PHP:
/*
* WPCS: show selected pages in their own currency, whatever the visitor chose.
* Child theme functions.php or a code snippets plugin.
*/
function my_page_currency() {
// page or post (ID or slug) => currency code from the Currencies tab
$map = array(
'uk-offer' => 'GBP',
'eu-offer' => 'EUR',
123 => 'USD',
);
foreach ($map as $page => $currency) {
if (is_page($page) || is_single($page)) {
return $currency;
}
}
return '';
}
function my_force_page_currency() {
global $WPCS;
$currency = my_page_currency();
if (empty($WPCS) || $currency === '') {
return;
}
$currencies = $WPCS->get_currencies();
if (isset($currencies[$currency])) {
$WPCS->current_currency = $currency;
}
}
// Before the page is built (block themes render the content before wp_head)
add_action('template_redirect', 'my_force_page_currency');
// Again after WPCS has applied GeoIP rules (its wp_head runs at priority 1)
add_action('wp_head', 'my_force_page_currency', 2);
// "I am using cache plugin on my site" = Yes: do not redraw the prices
// of these pages in the visitor's own currency after the page has loaded
add_action('wp_head', function () {
if (my_page_currency() !== '') {
echo "<script>window.wpcs_shop_is_cached = 0;</script>\n";
}
}, 3);
Every price on the listed pages, the switcher and the side switcher now use the page's currency. The snippet changes only $WPCS->current_currency, the currency of the current page view, and leaves the visitor's saved choice alone. The next page they open is in their own currency again, so no "else" branch is needed. Because a forced page looks the same for every visitor, it can stay in your page cache.
When the page should switch the visitor
Snippets for this task often also call $WPCS->storage->set_val(). That writes the currency into the visitor's saved choice, so it follows them to every other page. It is the right behaviour for a country landing page that should switch the visitor for the whole visit; add this line next to the current_currency line:
$WPCS->storage->set_val('wpcs_current_currency', $currency);
Exclude such pages from the page cache: the PHP code has to run for each visitor to save the currency. Do not add an "else" branch that sets a currency on all other pages: it would take the switcher away from visitors on the whole site.
Hide the switcher on forced pages
A switcher on a page that ignores it only confuses visitors. For the side switcher, open the Side switcher tab and enter the page IDs in Hide on the pages, separated by commas and without spaces: 28,34,232. The check uses WordPress's is_page(), so it matches pages only, not posts or custom post types; the words home and front_page are also accepted.
A [wpcs] switcher you placed yourself, in a header template or a widget, has to be left out on those pages in the same place. In a template, the function from the snippet does the check:
<?php
// Child theme header.php: no switcher on pages with a forced currency
if (!function_exists('my_page_currency') || my_page_currency() === '') {
echo do_shortcode('[wpcs]');
}
?>
Amounts are still written in the base currency
Forcing a currency changes how prices are displayed, not what the numbers in your shortcodes mean. On a page forced to GBP, [wpcs_price value=100] still means 100 in the base currency, converted to pounds. If the UK page should show exactly 49 pounds:
- write the price as plain text, which is fine on a page that never switches;
- or use
[wpcs_price type=fixed value=GBP:49,USD:59,EUR:55], with an amount for every currency (see fixed prices per currency); - or convert from pounds with the small
[price_from]shortcode from how the base currency works.
Checklist
- One price in a set currency:
fix_currency, with the cache option off and never withtype=fixed. - Ads and campaign links:
?currency=XXXon the page address. - Whole pages: the snippet, with the codes from the Currencies tab and the right page IDs or slugs.
- Switchers hidden on forced pages: Hide on the pages without spaces, plus your own
[wpcs]placements. - Tested in a private window after clearing the cache, then a normal page opened to confirm the visitor's own currency is back.
If a page still shows the wrong currency, open a topic on the WPCS support forum with the page link, the snippet you use and your cache settings.