and like us on FB

When we read traditional news media, financials are usually defined in US dollars and then converted to a local currency for comparison. The same feature is usually found on e-commerce stores where a currency is rendered as a US value and then converted for a specific customer (in a particular region). Since we provide so many finance brokers, real-estate organisations, and shop owners with our marketing and social services, it’s only natural that we’d provide them with a means of converting currency.

In traditional newspapers, the currency comparison is fixed — defined by the conversion at the time the story was published. In the new media world, we tend to extract as much functionality from third-party API’s and other providers to ensure real-time information. The following functions are another means of ensuring sites remain current and relevant.

The following function (and the associated WordPress shortcode function) will use Google’s finance ‘calculator’ to convert currency and return the comparative result in local dollars. The use of Google’s tool is done so simply for the sake of the demonstration. Our clients will be provided with a more robust API that can be relied upon without resorting to dodgy regular expressions to parse HTML. The next currency post uses a more suitable public source.

NOTE: The original code we provided (on Internoetics) stopped working when Google shut down the iGoogle service. While we updated it with a new Google source, that doesn’t mean it won’t cease to be available in the future. Google shutting down its original currency API demonstrates again why shortcodes are so awesome. Since the sourced changed, we simply updated a single function to restore global functionality.

The Result

Used in a WordPress post, the shortcode of [conversion amount="750"] will output $946.8 AUD. The $750 is the USD value while the returned value is generated in real time and represents the local dollar value. The shortcode of [conversion amount="1"] will output $1.26 AUD. If I wanted to convert 1 Australian Dollar to NZ dollars, I would use [conversion from="AUD" to="NZD" amount="1"] and it would output $1.37 NZD.

For larger numbers, you might consider applying the number format (as described below). The primary purpose of the number format is to add a thousand separator. So, $12542155.3254 USD will translate to $15′833′216.88 AUD using [conversion amount="12542155.3254" n="1"].

Note: For the sake of the demonstration, values on this page may be cached for longer than the default 6 hours. The bold results are our own and aren’t applied by default.

Shortcode Attributes

Available shortcode parameters used to alter the results are as follow.

from – The from currency.
to – The currency we’re converting to.
amount – the dollar value to convert.
dec – Number of decimal values. Defaults to 2.
n – Use number format? Defaults to false.
cache – the cache period. Defaults to 21600 (6 hours).

If the number format applies, the following options may be used (number_format() parameters).

dp – Character to use as a decimal point. Defaults to a period.
ts – Thousands seperator. Defaults to a single prime character (′) character.

The list of three-letter currency codes (ISO 4217) are listed on Wikipedia .

The Shortcode Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

You may optionally install the standalone plugin (available for download from the bottom of the page) via your control panel or FTP.

Basic PHP Function

Used outside of WordPress the following function may be used.

This code requires the use of our 'Simple Cache' functions. Review its use here or download the code here.

Considerations

  • If you’re a client, we use our own source of currency values. The Google source is used for demonstration purposes. Download from the platform plugin area.
  • The client plugin has an additional feature that will print all available currencies on a single page.
  • Again, the shortcode is really only to be used when converting a single currency value (say, when providing product reviews and noting a price in local currency). We’ll provide another function soon that deals with multiple conversions.
  • Error checking should normally be performed on the returned data and result.

Video Introduction

Download

Title: Convert Currency in WordPress or With PHP
Description: Convert currency in WordPress or with PHP.
Download: Shortcode (V0.2) | WP Plugin (V0.1) | PHP Code (V0.2)

Shortt URL for this post: http://shor.tt/Zco