The code in this article is to be considered a customisation. As such, it's outside the scope of our support service. Should you need assistance implementing the code, please feel free to contact us to request a consultation.


You can alter the text displayed on the widget with a simple filter, which you can put in your theme's functions.php file. See How to Safely Add Custom Code to WordPress Sites for more information on how to do that.


1. How to set the currency labels to arbitrary values

/**
 * Alters the currency labels that will be displayed on the currency selector widget.
 * 
 * @param array currencies An array of currency code => currency label pairs.
 * @param string widget_type The type of widget beind displayed (e.g. 'dropdown' or 'buttons').
 * @param string widget_title The widget title
 * @param string widget_template_name The template file that is going to be used to render the widget.
 * @return array
 */
add_filter('wc_aelia_currencyswitcher_widget_currency_options', function($currencies, $widget_type, $widget_title, $widget_template_name) {
  $currencies['USD'] = 'My label for USD';
  $currencies['GBP'] = 'My label for GBP';

  return $currencies;
}, 10, 4); 

 

2. How to replace the currency labels with currency codes

/**
 * Replaces the currency labels that will be displayed on the currency selector widget with
 *  the currency codes
 * 
 * @param array currencies An array of currency code => currency label pairs.
 * @param string widget_type The type of widget beind displayed (e.g. 'dropdown' or 'buttons').
 * @param string widget_title The widget title
 * @param string widget_template_name The template file that is going to be used to render the widget.
 * @return array
 */
add_filter('wc_aelia_currencyswitcher_widget_currency_options', function($currencies, $widget_type, $widget_title, $widget_template_name) {
  foreach($currencies as $currency_code => $label) {
    $currencies[$currency_code] = $currency_code;
  }
  return $currencies;
}, 10, 4);


3. Localisation

Please note that the examples below are provided for guidance, and that they may require adjustments to work in your specific environment. If you need help translating strings with WPML, please contact the support team for the localisation plugin you use.


3.1 WPML

To translate the custom description using WPML, please follow the instructions on their website: Getting string translation to work (section 2, Registering strings manually). You can find an example of how to implement the filters below. 


Step 1 - Register the currencies for translation

You will first have to register your strings in WPML, as follows:     

/**
 * Register the custom currency names with WPML on initialisation.
 */
function register_currency_labels() {
  $context = 'currency_selector_widget';
  icl_register_string($context, 'EUR', 'Euro');
  icl_register_string($context, 'GBP', 'Pound Sterlings');
  icl_register_string($context, 'USD', 'US Dollars');
}
add_action('init', 'register_currency_labels', 15);

This will make the text available for translation in WPML Control Panel. 


Step 2 - Call the translation functions

Following the example from the filter at the top of the article, you will have to invoke the WPML localisation functions, so that the text can be translated automatically, as follows:  

/**
 * Alters the currency labels that will be displayed on the currency selector widget. This filter
 * invokes WPML functions to translate the new labels.
 * 
 * @param array currencies An array of currencies passed by the widget.
 * @param string widget_type The type of widget beind displayed (e.g. 'dropdown' or 'buttons').
 * @param string widget_title The widget title
 * @param string widget_template_name The template file that is going to be used to render the widget.
 * @return array
 */
function my_currency_labels($currencies, $widget_type, $widget_title, $widget_template_name) {
  $context = 'currency_selector_widget';

  $currencies['EUR'] = icl_t($context, 'EUR', 'Euro');
  $currencies['GBP'] = icl_t($context, 'GBP', 'Pound Sterlings');
  $currencies['USD'] = icl_t($context, 'USD', 'US Dollars');

  return $currencies;
}
add_filter('wc_aelia_currencyswitcher_widget_currency_options', 'my_currency_labels', 10, 4); 


3.2 Polylang

The Polylang plugin works in a similar way to WPML, it just calls different functions. 


Step 1 - Register your custom labels

/**
 * Register the custom currency names with Polylang on initialisation.
 */
function register_currency_labels() {
  $context = 'currency_selector_widget';
  pll_register_string('EUR', 'Euro', $context);
  pll_register_string('GBP', 'Pound Sterlings', $context);
  pll_register_string('USD', 'US Dollars', $context);
}
add_action('init', 'register_currency_labels', 15);

   

Step 2 - Call the Polylang translation functions in the custom filter for the currency selector widget

/**
 * Alters the currency labels that will be displayed on the currency selector widget. This filter
 * invokes Polylang functions to translate the new labels.
 * 
 * @param array currencies An array of currencies passed by the widget.
 * @param string widget_type The type of widget beind displayed (e.g. 'dropdown' or 'buttons').
 * @param string widget_title The widget title
 * @param string widget_template_name The template file that is going to be used to render the widget.
 * @return array
 */
function my_currency_labels($currencies, $widget_type, $widget_title, $widget_template_name) {
  $currencies['EUR'] = pll__('Euro');
  $currencies['GBP'] = pll__('Pound Sterlings');
  $currencies['USD'] = pll__('US Dollars');

  return $currencies;
}
add_filter('wc_aelia_currencyswitcher_widget_currency_options', 'my_currency_labels', 10, 4); 

  

4. Sorting

By default, the currencies are sorted by currency code. You might want to ensure that the currencies listed in the dropdown are sorted alphabetically, no matter what language is active. You do that by writing a filter for the wc_aelia_currencyswitcher_widget_currency_options hook, as follows. 


4.1 Sorting by description

/**
 * Sorts the currency labels alphabetically.
 * 
 * @param array currencies An array of currency code => currency label pairs.
 * @param string widget_type The type of widget beind displayed (e.g. 'dropdown' or 'buttons').
 * @param string widget_title The widget title
 * @param string widget_template_name The template file that is going to be used to render the widget.
 * @return array
 */
function my_sorted_currency_labels($currencies, $widget_type, $widget_title, $widget_template_name) {
  // Sort the list of currencies by name
  asort($currencies);

  return $currencies;
}
add_filter('wc_aelia_currencyswitcher_widget_currency_options', 'my_sorted_currency_labels', 20, 4); 

4.2 Custom sorting

/**
 * Sorts the currency in a custom order.
 * 
 * @param array currencies An array of currency code => currency label pairs.
 * @param string widget_type The type of widget beind displayed (e.g. 'dropdown' or 'buttons').
 * @param string widget_title The widget title
 * @param string widget_template_name The template file that is going to be used to render the widget.
 * @return array
 */
function my_sorted_currency_labels($currencies, $widget_type, $widget_title, $widget_template_name) {
  // This array will specify the order of the currencies. We set the keys
  // (currency codes) and leave the labels empty, as they will be taken from
  // the original currency list
  $sorted_currencies = array(
    'USD' => '',
    'EUR' => '',
    'GBP' => '',
  );
  
  // Merging the original list with our custom one will preserve our order, but
  // take the correct currency labels
  return array_merge($sorted_currencies, $currencies);
}
add_filter('wc_aelia_currencyswitcher_widget_currency_options', 'my_sorted_currency_labels', 20, 4); 

You can purchase the Currency Switcher from our online shop.