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.
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
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
value
is the currency value. For example, 1 for $1, or 20 for $20.btc
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
decimals
to truncate the returned bitcoin value.cache
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: AUD149570.09
Last value: AUD149570.09
Buy price: AUD149570.09
Sell price: AUD149570.09
Bitcoin value in HKD (HKD) currency.
15 minute market price: HKD762216.44
Last value: HKD762216.44
Buy price: HKD762216.44
Sell price: HKD762216.44
Bitcoin value in USD (USD) currency.
15 minute market price: USD97931.05
Last value: USD97931.05
Buy price: USD97931.05
Sell price: USD97931.05
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: 149570.09
Last value: 149570.09
Buy price: 149570.09
Sell price: 149570.09
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
currency="AUD"
. The currency must be supported by Blockchain..include
include="AUD,USD"
, where those currencies defined are the ones to be rendered.cache
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)