Localized Currency Amount Shortcode
Adds a [wpide_money] shortcode that formats an amount in a currency using the conventions of the current language, like $1,234.50, 1.234,50 € or ¥1,235.
Opens WPIDE on
Code
add_shortcode( 'wpide_money', function ( $atts ) {
$atts = shortcode_atts( array( 'amount' => 0, 'currency' => 'USD' ), $atts, 'wpide_money' );
$amount = (float) $atts['amount'];
$currency = strtoupper( preg_replace( '/[^A-Za-z]/', '', $atts['currency'] ) );
if ( class_exists( 'NumberFormatter' ) ) {
$formatter = new NumberFormatter( determine_locale(), NumberFormatter::CURRENCY );
$output = $formatter->formatCurrency( $amount, $currency );
if ( false !== $output ) {
return esc_html( $output );
}
}
return esc_html( $currency . ' ' . number_format_i18n( $amount, 2 ) );
} );
Notes
Use [wpide_money amount="1234.5" currency="EUR"]. Needs PHP's intl extension for full formatting; without it a simple fallback is used.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.