How to Display Current Currency Code with Custom Shortcode
Learn how to create a custom shortcode that displays only the currency code (e.g., “USD”, “EUR”, “GBP”) without any additional text.
The Problem
WPCS has a built-in shortcode [wpcs_current_currency] that displays the current currency, but it outputs:
Current currency is: USD
What you often need: Just the currency code: USD
This is useful when:
- Sending currency data to external applications
- Displaying currency in your own custom text
- Integrating with forms or other plugins
- Building custom pricing displays
Solution: Create Custom Shortcode
Add this code to your child theme’s functions.php or use Code Snippets plugin:
Basic Version (Currency Code Only)
/**
* Custom shortcode to display current currency code
* Usage: [current_currency]
* Output: USD (or EUR, GBP, etc.)
*/
add_shortcode('current_currency', function() {
if (class_exists('WPCS')) {
global $WPCS;
return $WPCS->current_currency;
}
return '';
});
Usage:
[current_currency]
Output:
USD
Extended Version (With Multiple Options)
This version gives you more flexibility with different output formats:
/**
* Extended currency shortcode with options
* Usage: [get_currency info="code"]
* Options:
* - code: USD
* - symbol: $
* - name: US Dollar
*/
add_shortcode('get_currency', function($atts) {
if (!class_exists('WPCS')) {
return '';
}
global $WPCS;
// Default attributes
$atts = shortcode_atts(array(
'info' => 'code', // code, symbol, or name
), $atts);
$currency = $WPCS->current_currency;
// Get currency data
$currencies = $WPCS->get_currencies();
switch ($atts['info']) {
case 'symbol':
// Return currency symbol ($, €, £)
return isset($currencies[$currency]['symbol']) ? $currencies[$currency]['symbol'] : '';
case 'name':
// Return currency name (US Dollar, Euro, etc.)
return isset($currencies[$currency]['name']) ? $currencies[$currency]['name'] : '';
case 'code':
default:
// Return currency code (USD, EUR, GBP)
return $currency;
}
});
Usage examples:
Currency code: [get_currency info="code"] Currency symbol: [get_currency info="symbol"] Currency name: [get_currency info="name"]
Output:
Currency code: USD Currency symbol: $ Currency name: US Dollar
Where to Add the Code
Option 1: Child Theme functions.php (Recommended)
- Go to Appearance → Theme Editor
- Select Child Theme (important!)
- Open
functions.php - Paste code at the end of file
- Click Update File
Why child theme:
- Parent theme updates won’t erase your code
- Safer and more maintainable
Option 2: Code Snippets Plugin (Easier)
- Install Code Snippets plugin: https://wordpress.org/plugins/code-snippets/
- Go to Snippets → Add New
- Paste code
- Set to “Run everywhere”
- Activate snippet
Advantages:
- No theme file editing
- Easy to enable/disable
- Survives theme changes
Real-World Use Cases
Use Case 1: Custom Pricing Text
Instead of:
Current currency is: USD Price: $99
You can write:
Price in [current_currency]: $99
Output:
Price in USD: $99
Use Case 2: Integration with Forms
Send currency code to external application via hidden field:
<form>
<input type="hidden" name="currency" value="[current_currency]">
<input type="text" name="amount">
<button type="submit">Submit</button>
</form>
Use Case 3: Conditional Content
Combine with conditional shortcode plugins:
[if currency="USD"] Special offer for US customers! [/if]
Use Case 4: Dynamic Text
Your cart total in US Dollar: $99.00
Output:
Your cart total in US Dollar: $99.00
Use Case 5: Multi-Currency Comparison
Price: $99 [current_currency] (Approximately €90 EUR or £80 GBP)
Advanced: Get GeoIP Detected Currency
If you want to show the currency that was auto-detected by GeoIP (not necessarily the current active currency):
/**
* Get GeoIP detected currency
* Usage: [geoip_currency]
*/
add_shortcode('geoip_currency', function() {
if (!class_exists('WPCS')) {
return '';
}
global $WPCS;
// Get currency detected by GeoIP
$geo_currency = $WPCS->get_currency_by_country(
$WPCS->get_user_country_code()
);
return $geo_currency ? $geo_currency : $WPCS->default_currency;
});
Use case: Show user what currency was detected based on their location:
We detected you're browsing from [geoip_currency] region. Currently viewing prices in [current_currency].
Advanced: Get All Currency Information
Get all data about current currency:
/**
* Get comprehensive currency data
* Usage: [currency_info]
*/
add_shortcode('currency_info', function() {
if (!class_exists('WPCS')) {
return '';
}
global $WPCS;
$currency = $WPCS->current_currency;
$currencies = $WPCS->get_currencies();
$data = $currencies[$currency];
$output = sprintf(
'Code: %s | Symbol: %s | Name: %s | Rate: %s',
$currency,
$data['symbol'],
$data['name'],
$data['rate']
);
return $output;
});
Output:
Code: EUR | Symbol: € | Name: Euro | Rate: 0.92
Styling the Output
You can wrap output in HTML for styling:
add_shortcode('styled_currency', function() {
if (!class_exists('WPCS')) {
return '';
}
global $WPCS;
$currency = $WPCS->current_currency;
return '<span class="currency-badge">' . esc_html($currency) . '</span>';
});
Then add CSS:
.currency-badge {
background: #007bff;
color: white;
padding: 2px 8px;
border-radius: 3px;
font-size: 12px;
font-weight: bold;
}
Output:
<span style="background:#007bff;color:white;padding:2px 8px;border-radius:3px;font-size:12px;font-weight:bold;">USD</span>
Troubleshooting
Shortcode Shows Nothing
Problem: [current_currency] displays blank
Possible causes:
- WPCS not active
- Verify WPCS plugin is installed and activated
- Check Plugins page
- Code not added correctly
- Verify code is in functions.php or Code Snippets
- Check for PHP syntax errors
- Look at error log (if WP_DEBUG enabled)
- Shortcode name conflict
- Another plugin might use same shortcode name
- Try different name:
[my_currency]instead
Solution: Add error checking to your shortcode:
add_shortcode('current_currency', function() {
if (!class_exists('WPCS')) {
return '<!-- WPCS plugin not found -->';
}
global $WPCS;
if (!isset($WPCS->current_currency)) {
return '<!-- Currency not set -->';
}
return $WPCS->current_currency;
});
View page source to see error message.
Wrong Currency Displayed
Problem: Shortcode shows wrong currency
Possible causes:
- Cache issue
- Clear all caches (WordPress, browser, server)
- Test in incognito mode
- Currency not switched yet
- User hasn’t switched currency manually
- GeoIP detection not working
- Check WPCS settings
Solution:
- Clear caches
- Test currency switching manually
- Verify GeoIP is configured in WPCS
Shortcode Works in Posts But Not Widgets
Problem: Shortcode displays in posts but not sidebar widgets
Cause: WordPress doesn’t process shortcodes in widgets by default
Solution: Add this code to enable shortcodes in widgets:
add_filter('widget_text', 'do_shortcode');
Special Characters Display Incorrectly
Problem: Currency symbols like € or £ show as �
Solution: Use esc_html() to properly encode:
add_shortcode('current_currency', function() {
if (class_exists('WPCS')) {
global $WPCS;
return esc_html($WPCS->current_currency);
}
return '';
});
Using in PHP Templates
If you need to use this in PHP template files instead of shortcodes:
<?php
if (class_exists('WPCS')) {
global $WPCS;
echo esc_html($WPCS->current_currency);
}
?>
Or with the shortcode:
<?php echo do_shortcode('[current_currency]'); ?>
Best Practices
- Always check if WPCS exists
if (class_exists('WPCS')) { // Your code }This prevents errors if WPCS is deactivated
- Use unique shortcode names
- Avoid generic names like
[currency] - Use prefixed names:
[my_site_currency]
- Avoid generic names like
- Escape output
return esc_html($WPCS->current_currency);
Prevents XSS vulnerabilities
- Test after WPCS updates
- Plugin updates might change API
- Test your custom shortcodes after updates
- Document your shortcodes
- Keep notes of what each shortcode does
- Add comments in code
Alternative: Using WPCS Built-in Function
Instead of creating shortcode, you can use WPCS function directly in PHP:
$currency = wpcs_current_currency(); echo $currency; // Outputs: USD
Available functions:
wpcs_current_currency()– Get current currency codewpcs_convert_price($amount, $from, $to)– Convert between currencieswpcs_get_user_currency()– Get user’s selected currency
Summary
Quick solution (copy-paste ready):
/**
* Display current currency code
* Usage: [current_currency]
* Output: USD (or EUR, GBP, etc.)
*/
add_shortcode('current_currency', function() {
if (class_exists('WPCS')) {
global $WPCS;
return esc_html($WPCS->current_currency);
}
return '';
});
Add to:
- Child theme’s
functions.php, OR - Code Snippets plugin
Use anywhere:
[current_currency]
Output:
USD
That’s it! Simple, clean, and works everywhere shortcodes are supported.
