bitcoin-average-wordpress-shortcode

Add a Bitcoin Marquee (Ticker) in WordPress with Shortcode

BitcoinAverage is a service providing the “BitcoinAverage Price Index”, a weighted average Bitcoin price across all reachable Bitcoin exchanges around the world. Currently over 40 exchanges are integrated, feeding nearly all available Bitcoin trading volume data. The BitcoinAverage Price Index is offered in over 150 currencies.

The website offers a free API with very few restrictions. Their API reference page says that “… there is no explicit restriction on how often you can call the API, however calling it more than once a minute makes no sense. Please be good”. While the Bitcoin price can vary wildly in the space of just a couple of minutes it still makes more sense with WordPress to cache it for longer periods to avoid making unnecessary and lengthy requests.

In this, our second article on various Bitcoin APIs, we’ll be adding a Bitcoin ticker to your WordPress website with shortcode. While there are numerous applications that might be considered given the comprehensive BtcoinAverage data (including historical CSV files) we’ll be making a simple request on this occasion, but we’ll certainly revisit the API in the future.

The HTML Marquee Tag

While there are a number of awesome ticker-type options made available with jQuery and native JavaScript – combined with some groovy CSS styling – we’re going to avoid that fancy-pants stuff and use nothing other than the HTML Marquee tag for the purpose of simplicity.

The marquee element provides a way for browsers to render text that moves across the page without having to resort to JavaScript techniques. The marquee is supported in virtually all browsers (despite the tag essentially being obsolete) although they all tend to provide their own subtle variations in the way its rendered, making the appearance alter slightly from one browser to another.

The marquee attributes are detailed in the section below on “Shortcode Attributes”. We’ll look at some styled tickers in the future.

The Result

A basic marquee ticker displaying currencies for Australia, USA, New Zealand, and China, is obtained via the shortcode of [bitcoinaverage]. While around 150 currencies are returned via the API, we’ve limited the returned data by way of a default value in the shortcode. The result:

฿ BTC Trading : AUD 24HR average $ Ask $1649.37 Bid $1646.05 Last $1648.7 Volume BTC 27730.82 Volume % 0.67 NZD 24HR average $ Ask $1769.96 Bid $1766.4 Last $1769.24 Volume BTC 27730.82 Volume % 0.07 USD 24HR average $ Ask $1246.32 Bid $1243.81 Last $1245.81 Volume BTC 27730.82 Volume % 59.79

The Shortcode Function

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.

Shortcode Attributes

currency

The API returns around 150 currencies – not all of which you’ll want to display in your marquee. If the currency attribute is left empty it will return them all.. but it’s likely that you will want to limit returned data relevant to your audience. If you want to display all data, you should do so on a dedicated page – not a marquee or ticker. In my case I return the currency in AUD, NZD, USD, and CHN (Chinese Yuan) using the shortcode of [bitcoinaverage currency="aud,$|nzd,$|usd,$|chn,¥"]. The format of each requested currency is the three letter country code followed by a comma, and then the HTML entity (or symbol) for the currency symbol. Each country/currency pair is separated by a p|pe character. Find currency HTML entity codes here.

behavior

Sets how the text is scrolled within the marquee. Possible values are scroll, slide and alternate. If no value is specified, the default value is scroll.

loop

Sets the number of times the marquee will scroll. If no value is specified, the default value is 1, which means the marquee will scroll continuously. Since this support doesn’t seem to be globally recognised we use 100.

direction

Sets the direction of the scrolling within the marquee. Possible values are left, right, up and down. If no value is specified, the default value is left.

height

Sets the height in pixels or percentage value.

hspace

Sets the horizontal margin.

scrollamount

Sets the amount of scrolling at each interval in pixels. The default value is 6.

scrolldelay

Sets the interval between each scroll movement in milliseconds. The default value is 85. Note that any value smaller than 60 is ignored and the value 60 is used instead, unless truespeed is specified.

width

Sets the width in pixels or percentage value.

cache

The number of seconds the API data will be cached locally before a request for fresh information is made. Defaults to 900 (15 minutes).

Considerations

The following pages serve as references for BitcoinAverage:

We’ll be providing additional articles on working with the BitcoinAverage API in the future. Be sure to subscribe to our lonely Facebook page for updates.

Download the Code

Download the shortcode function here .

Shortt URL for this post:

Leave a Reply

Your email address will not be published. Required fields are marked *