When we read traditional news media, financials are usually defined in US dollars and then converted to a local currency for comparison. The following function (and the associated WordPress shortcode function) will use Google’s “calculator” to convert currency (using a CURL request) and return the comparative result in local dollars.
In traditional newspapers, the currency comparison is usually 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 blogs remain current and relevant.
The shortcode
First, the shortcode. Used in a WordPress post, the shortcode of [conversion amount=”750″] will output $USD750 ($AUD764.53). The $750 is the USD value while the value in brackets is generated in real time and represents the local dollar value.
Available parameters are as follows:
from – The from currency
to – The currency we’re converting into
amount – the dollar value to convert
time – the cache period
To use the function, copy the following into your theme’s functions.php file or add it into your shortcode plugin.
// Currency Conversion
function currency_conversion($atts) {
extract(shortcode_atts(array(
'from' => 'USD',
'to' => 'AUD',
'amount' => '',
'time' => '21600'
), $atts));
$currencytransient = 'conv_' . $from . $to . $amount;
$cachedresult = get_transient($currencytransient);
if ($cachedresult !== false ) {
return $cachedresult;
} else {
$amount = urlencode($amount);
$from_Currency = urlencode($from);
$to_Currency = urlencode($to);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
$conversion = '$'.$from.$amount.' ($'.$to. round($var,2) . ')';
set_transient($currencytransient, $conversion, 60*60*12);
update_option($currencytransient, $conversion);
return $conversion;
}
}
add_shortcode('conversion', 'currency_conversion');
By default, the result is cached for 6 hours. You can alter this as required by changing the time parameter. Used on financial websites, it gives you a nice way of presenting a single dollar conversion. As an example, the shortcode of [conversion amount=”1″] will output $USD1 ($AUD1.02). 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 $USD1 ($NZD1.22).
Basic PHP Function
Used outside of WordPress, the same function can be used as follows:
<?php
function currency($from_Currency,$to_Currency,$amount) {
$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return round($var,2);
}
?>
Usage:
$from_Currency ="USD";
$to_Currency ="AUD";
$amount ="1000";
echo currency($from_Currency,$to_Currency,$amount);
You will need to alter with the code a little to ensure it fomats in an appropriate manner. Download the code in a text file below.
|
|
Download: Currency Conversion Functions (PHP and WP Shortcode) |
If you liked this article, you may also like:
- Generate QR Codes on Your Website or WordPress Post with Google Chart Tools
- Website Snapshots with WordPress Shortcode
- PayPal Donate Text or Button in a WordPress Post with Shortcode
- [Shortcode] Easily post an aviation Metar report into your post or page with shortcode
- [Shortcode] Easily post an aviation TAF report into your WordPress post or page with shortcode


Thanks, marty! for such a nice php script. But one thing i can’t understand whenever you used this script with INR i.e, USD to INR conversion it work fine below 19 and if you convert 20 USD to INR then it only shows only 1st digit of the resultant output. Do you know why this happens?
i want full resultant, like this:
http://www.google.com/ig/calculator?hl=en&q=20USD=?INR