@talha6248 This may be of use to you, I did something similar. In my case I wanted to show the donation goals and current donation level in local currencies for people visiting our other country sites.
I wrote a small function to query google API and get the exchange rates. One thing to note though is to better do this in a cron once a day or so and save it locally in the DB. Querying API for every conversion slowed down the site drastically, so I don’t recommend that.
Alternatively, if you could do it over javascript, so it runs in the users browser only, then it should be fine to use API. I assume that’s what you want to do anyways, as it sounds you just want to convert some input field.
Anyways, for my use, since I just wanted 2 additional currencies and processing done server side, I just misused the options table to store currencies ??
You could do something like this:
function tow_cron_activation() {
if (! wp_next_scheduled ( 'get_currency_exchange_rate' )) {
wp_schedule_event(time(), 'daily', 'get_currency_exchange_rate');
}
}
register_activation_hook(__FILE__, 'tow_cron_activation');
function tow_cron_deactivation() {
wp_clear_scheduled_hook('get_currency_exchange_rate' );
}
register_deactivation_hook(__FILE__, 'tow_cron_deactivation');
function tow_exchange_rate() {
$prev_rate=get_option('tow_donation_exchange_rate');
$rate=array('timestamp' => time() );
foreach( array("AUD", "INR") as $currCode) {
$get = file_get_contents("https://finance.google.com/finance/converter?a=1&from=USD&to=".urlencode($currCode));
$get = explode("<span class=bld>",$get);
$get = explode("</span>",$get[1]);
$new_rate = preg_replace("/[^0-9\.]/", null, $get[0]);
if (! $new_rate > 0) {
$new_rate = $prev_rate['INR'];
}
$rate[$currCode]=$new_rate;
}
update_option('tow_donation_exchange_rate', $rate);
}
add_action('get_currency_exchange_rate', 'tow_exchange_rate');
Then you just would need to do something like get_option(‘tow_donation_exchange_rate’); to get the rates.
It’s a quick hack, but does the job.