and like us on FB

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 0.00020148. To return a Bitcoin value for a USD we’ll use [blockchaintxt currency="USD"]. The result: 0.00025206.

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 ($4963.27) 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 3962.20.

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.

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 USD ($) currency.

15 minute market price: $3964.48
Last value: $3964.48
Buy price: $3964.79
Sell price: $3964.17

Bitcoin value in AUD ($) currency.

15 minute market price: $4949.07
Last value: $4949.07
Buy price: $4949.46
Sell price: $4948.68

Bitcoin value in HKD ($) currency.

15 minute market price: $30939.64
Last value: $30939.64
Buy price: $30942.05
Sell price: $30937.22

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 ($) currency.

15 minute market price: 4952.68
Last value: 4952.68
Buy price: 4953.44
Sell price: 4951.93

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.

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 WordPress with the Blockchain API
Description: Convert currency into Bitcoin with the Blockchain API.
Download: Shortcode (V0.2) | WP Plugin (V0.2) | PHP Code (V0.2)

Shortt URL for this post: http://shor.tt/2gN6