RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Convert Bitcoin Currency in WordPress with the Blockchain API

Convert Bitcoin Currency in WordPress with the Blockchain API

Bitcoin is an "open source" peer-to-peer digital store of value and payment system that permits direct transfers without the need for an intermediary. Transactions are verified by network nodes and recorded in a public distributed ledger called the block chain. The ledger uses its own unit of account: the bitcoin. The system works without a central repository or single administrator, so it's referred to as a decentralized virtual currency... also referred to as a cryptocurrency. While there are dozens of sites that provide more information, Wikipedia or Bitcoin.org are good places to start.

A number of websites provide APIs that'll provide the current conversion value of Bitcoin. While I intended to include about a dozen different Bitcoin API functions on the one page, it was far too long. We'll write separate articles for different providers and the vasty different API services that they all provide.

Blockchain.info

Blockchain provides two APIs; one that returns a basic text response, and another that returns a JSON response. The latter provides more information while the former simply provides a converted value.

Two Way Conversions of Bitcoin Currency

Our first shortcode function returns just a returned conversion rate given a currency value. Our shortcode function will retrieve the value (based on shortcode attributes) and cache the result for 30 minutes so as to avoid repeated requests to Blockchain.

The function default values are set to Australia (AUD) and 1 (for AU$1). So, the shortcode of [blockchaintxt] will return a value of . To return a Bitcoin value for a USD we'll use [blockchaintxt currency="USD"]. The result: .

If you would like to get the value of a 1 Bitcoin, we use the attribute of btc="1" ("btc" being the bitcoin currency shorthand) which is a trigger for us do the conversion ourselves (the API doesn't convert in a backwards manner). So, 1 Bitcoin can be returned in Australian dollars ($) by using [blockchaintxt currency="AUD" btc="1"]. We can return the value of 2 bitcoins in USD by using [blockchaintxt currency="USD" value="2" btc="1"], with a returned result of .

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
 Convert Bitcoin Currency in WordPress with Shortcode (Blockshain Text File)
4
 http://www.beliefmedia.com/blockchain-bitcoin-wordpress
5
*/
6
 
7
8
 
9
  $atts = shortcode_atts(array(
10
    'currency' => 'AUD',
11
    'value' => '1',
12
    'btc' => false,
13
    'decimals' => '2',
14
    'cache' => 60 * 60 /* 30 minutes */
15
  ), $atts);
16
 
17
  $transient = 'bmbc_' . md5(serialize($atts));
18
  $blockchain = get_transient($transient);
19
  if ($blockchain !== false) {
20
   return $blockchain;
21
 
22
  } else {
23
 
24
  $val = ($atts['btc'] !== false) ? $atts['value'] : '1';
25
  $blockchain = file_get_contents('https://blockchain.info/tobtc?currency=' . strtoupper($atts['currency']) . '&value=' . $val);
26
  $return = ($atts['btc'] !== false) ? bcdiv($atts['value'], $blockchain, $atts['decimals']) : $blockchain;
27
  set_transient($transient, $return, $atts['cache']);
28
  return $return;
29
 
30
 }
31
}
32
add_shortcode('blockchaintxt', 'beliefmedia_blockchain_bitcoin_text');

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.

Available attributes are as follows:

currency

The currency is the default currency that you choose to use. Use others by way of currency="USD" or update the code for a new default value.

value

The value is the currency value. For example, 1 for $1, or 20 for $20.

btc

The btc attribute determines which way we're converting. By default we'll input a value and get the equivalent value of bitcoin. To using btc="1" (true) means that the value (above) is now the value of the bitcoin and we'll return the matching currency value (specified by currency).

decimals

Use decimals to truncate the returned bitcoin value.

cache

The cache determines how long the result will be stored locally. 30 minutes by default.

Bitcoin Conversions for Multiple Currencies with Sell and Buy Price

Blockchain optionally provides for a JSON object to be returned with the buy and sell rates for a number of currencies in the preceding 15 minute window. The example below is a fairly crude example of the output (it's expected that you'll style yourself). I've outputted just a few currencies for the purpose of page formatting but you can see the full rendering here.

Bitcoin value in AUD (AUD) currency.

15 minute market price: AUD99923.65
Last value: AUD99923.65
Buy price: AUD99923.65
Sell price: AUD99923.65

Bitcoin value in HKD (HKD) currency.

15 minute market price: HKD502082.02
Last value: HKD502082.02
Buy price: HKD502082.02
Sell price: HKD502082.02

Bitcoin value in USD (USD) currency.

15 minute market price: USD64110.99
Last value: USD64110.99
Buy price: USD64110.99
Sell price: USD64110.99

I used the shortcode of [blockchaindata include="AUD,USD,HKD"]. The include="AUD,USD,HKD" is a comma separated list of currencies if you choose to discriminate against some and only render a few - by default they'll all print to your screen.

The result of a single location can be displayed with the shortcode of [blockchaindata currency="AUD"]. The result:

Bitcoin values for AUD (AUD) currency.

15 minute market price: 99923.65
Last value: 99923.65
Buy price: 99923.65
Sell price: 99923.65

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

1
<?php 
2
/*
3
 Convert Bitcoin Currency in WordPress with Shortcode (Blockshain Text File)
4
 http://www.beliefmedia.com/blockchain-bitcoin-wordpress
5
*/
6
 
7
8
 
9
  $atts = shortcode_atts(array(
10
    'currency' => '',
11
    'include' => '',
12
    'cache' => 60 * 23 /* 30 minutes */
13
  ), $atts);
14
 
15
  $bcrecord = 'bmbt_' . md5(serialize($atts));
16
  $blockchain = get_transient($bcrecord);
17
  if ($blockchain !== false) {
18
   return $blockchain;
19
 
20
  } else {
21
 
22
  $data = file_get_contents('https://blockchain.info/ticker');
23
  $data = mb_convert_encoding($data, 'HTML-ENTITIES', "UTF-8");
24
  $data = json_decode($data, true);
25
 
26
  if ( ($atts['include'] == '') && ($atts['currency'] != '') ) {
27
 
28
    $currency = strtoupper($atts['currency']);
29
    $fifteen = $data["$currency"]['15m'];
30
    $last = $data["$currency"]['last'];
31
    $buy = $data["$currency"]['buy'];
32
    $sell = $data["$currency"]['sell'];
33
    $symbol= $data["$currency"]['symbol'];
34
 
35
    $return .= 'Bitcoin values for ' . $currency . ' (' . $symbol . ') currency.';
36
    $return .= '
37
<blockquote><p>15 minute market price: ' . $fifteen . '';
38
    $return .= 'Last value: ' . $last . '';
39
    $return .= 'Buy price: ' . $buy . '';
40
    $return .= 'Sell price: ' . $sell . '</p></blockquote>
41
 
42
';
43
 
44
  } else {
45
 
46
    /* Available currencies (used if include is empty) */
47
    $include = $atts['include'];
48
    $permitted = explode(',', $include);
49
 
50
    foreach ($data AS $key => $val) {
51
 
52
      if ( (!in_array($key, $permitted)) && ($include != '') ) continue;
53
 
54
        $fifteen = $val['15m'];
55
        $last = $val['last'];
56
        $buy = $val['buy'];
57
        $sell = $val['sell'];
58
        $symbol = $val['symbol'];
59
 
60
        $return .= 'Bitcoin value in ' . $key . ' (' . $symbol . ') currency.';
61
        $return .= '
62
<blockquote><p>15 minute market price: ' . $symbol  . '' . $fifteen . '';
63
        $return .= 'Last value: ' . $symbol  . '' . $last . '';
64
        $return .= 'Buy price: ' . $symbol  . '' . $buy . '';
65
        $return .= 'Sell price: ' . $symbol  . '' . $sell . '</p></blockquote>
66
 
67
';
68
      }
69
    }
70
 
71
  set_transient($bcrecord, $return, $atts['cache']);
72
  return $return;
73
 }
74
}
75
add_shortcode('blockchain', 'beliefmedia_blockchain_bitcoin_ticker');

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.

Available attributes are as follows:

currency

By design the function will return all currency. To limit the result to a single currency, use currency="AUD". The currency must be supported by Blockchain..

include

To output multiple values but only include certain values, use include="AUD,USD", where those currencies defined are the ones to be rendered.

cache

The cache determines how long the result will be stored locally. 30 minutes by default.

PHP Function

PHP functions to be used outside of WordPress are available as downloads below. Usage requires Simple Cache.

Considerations

  • If you plan on multiple requests, consider caching the blockchain data in its entirety.
  • While we have another post scheduled designed specifically to display a ticker, you can do the same with this data in company with our footer banner.

Download


Title: Convert Bitcoin Currency in WP with the Blockchain API (Plugin)
Description: Convert currency into Bitcoin with the Blockchain API.
  Download • Version 0.2, 1.9K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (1.2K)    PHP Code & Snippets, (1.1K)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment