Bitcoin Forum

Bitcoin => Project Development => Topic started by: michaelsuede on February 15, 2012, 04:48:43 PM



Title: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: michaelsuede on February 15, 2012, 04:48:43 PM
I just thought I would share this so that blog operators can easily add a modifiable BTC price widget to their Wordpress sites.

I'm not sure if this will work on a Wordpress.com hosted site, but it works fine on my stand-alone Wordpress installation.

High traffic blogs should take additional steps to cache the results from Mt. Gox or they may be throttled/blocked from updates by Mt. Gox.  However, this widget should be suitable for most blog operators that don't have a regular viewership of more than several thousand page views per day.

To make this work, install the "PHP Code Widget" from the plugin gallery.

Widget can be found here:
http://wordpress.org/extend/plugins/php-code-widget/ (http://wordpress.org/extend/plugins/php-code-widget/)

Then add the PHP Code Widget to your sidebar and paste this into the widget's text field.  You can modify the text to display anything you like:

Code:

You can contribute to this site by making Bitcoin donations to: <p><font color="red">xxxxxxxxxxxxxxxxx
</font><p/>Thanks for contributing!
<?php
        
//first fetch the current rate from MtGox
        
$ch curl_init('https://mtgox.com/api/0/data/ticker.php');
                
curl_setopt($chCURLOPT_REFERER'Mozilla/5.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
                
curl_setopt($chCURLOPT_USERAGENT"CakeScript/0.1");
                
curl_setopt($chCURLOPT_HEADER0);
                
curl_setopt($chCURLOPT_RETURNTRANSFER1);
                
curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
                
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
                
$mtgoxjson curl_exec($ch);
                
curl_close($ch);
               
        
//decode from an object to array
                
$output_mtgox json_decode($mtgoxjson);
                
$output_mtgox_1 get_object_vars($output_mtgox);
                
$mtgox_array get_object_vars($output_mtgox_1['ticker']);
 
?>

<br/>
<br/>
Last:&nbsp;<?php echo $mtgox_array['last'];   ?><br/>
High:&nbsp;<?php echo $mtgox_array['high'];   ?><br/>
Low:&nbsp;&nbsp;<?php echo $mtgox_array['low'];   ?><br/>
Avg:&nbsp;&nbsp;&nbsp;<?php echo $mtgox_array['avg'];   ?><br/>
Vol:&nbsp;&nbsp;&nbsp;<?php echo $mtgox_array['vol'];   ?><br/>


To see it in action (and to donate ;) ), visit my site:
http://www.libertariannews.org/


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: bitcoinspot.nl on February 15, 2012, 10:28:57 PM
Great,

i think i will tweak it a little and put it on www.bitcoinspot.nl

cheers!


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: michaelsuede on February 15, 2012, 10:48:23 PM
Great,

i think i will tweak it a little and put it on www.bitcoinspot.nl

cheers!

Thanks.

I was using those image widgets before but they kept on breaking because the site operators got lazy in maintaining them.

This widget obviously pulls directly from Mt. Gox's API.  So as long as Mt. Gox API is working, this widget will continue to work correctly.

I also like it because I can format it however I want.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: Clark on February 16, 2012, 07:06:40 AM
I would cache this result with a timeout of a minute or so. You might even make it do an AJAX callback so your page loads faster.

Caching is needed because if you get visitors more often than every 10 seconds, then MtGox might start throttling/blocking you.

You need to do it asynchronously so that every page load doesn't have to talk to MtGox's server before delivering the final page to the user.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: michaelsuede on February 16, 2012, 03:31:09 PM
I would cache this result with a timeout of a minute or so. You might even make it do an AJAX callback so your page loads faster.

Caching is needed because if you get visitors more often than every 10 seconds, then MtGox might start throttling/blocking you.

You need to do it asynchronously so that every page load doesn't have to talk to MtGox's server before delivering the final page to the user.

That's a good idea, but more work than I'm willing to put into it at the moment.

I suppose we can just leave this "as is" for low to medium traffic blogs, but it is worth noting that high traffic sites should cache these results.

If someone updates the code snippet to cache against a MySQL database, I'll update the OP to include install instructions and the relevant code.  Ideally this code should be wrapped in its own Wordpress widget and the MySQL script to setup the database cache should be set to install automatically with the install of the Widget from inside Wordpress, with the return variables from Mt. Gox being accessible by short code in the widget text field.  This way people who have no technical knowledge would still be able to easily install the script.

It might be worthwhile to not even limit the short code variable results to a widget, but rather make them accessible from anywhere on the site, such as in headers, footers or post content.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: Clark on February 16, 2012, 04:24:10 PM
That's a good idea, but more work than I'm willing to put into it at the moment.

I suppose we can just leave this "as is" for low to medium traffic blogs, but it is worth noting that high traffic sites should cache these results.

If someone updates the code snippet to cache against a MySQL database, I'll update the OP to include install instructions and the relevant code.  Ideally this code should be wrapped in its own Wordpress widget and the MySQL script to setup the database cache should be set to install automatically with the install of the Widget from inside Wordpress, with the return variables from Mt. Gox being accessible by short code in the widget text field.  This way people who have no technical knowledge would still be able to easily install the script.

It might be worthwhile to not even limit the short code variable results to a widget, but rather make them accessible from anywhere on the site, such as in headers, footers or post content.

It sounds like *somebody* needs to put together a suite of Wordpress plugins for various Bitcoin economic parameters: difficulty, # blocks, market cap, ticker.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: michaelsuede on February 16, 2012, 04:38:04 PM
That's a good idea, but more work than I'm willing to put into it at the moment.

I suppose we can just leave this "as is" for low to medium traffic blogs, but it is worth noting that high traffic sites should cache these results.

If someone updates the code snippet to cache against a MySQL database, I'll update the OP to include install instructions and the relevant code.  Ideally this code should be wrapped in its own Wordpress widget and the MySQL script to setup the database cache should be set to install automatically with the install of the Widget from inside Wordpress, with the return variables from Mt. Gox being accessible by short code in the widget text field.  This way people who have no technical knowledge would still be able to easily install the script.

It might be worthwhile to not even limit the short code variable results to a widget, but rather make them accessible from anywhere on the site, such as in headers, footers or post content.

It sounds like *somebody* needs to put together a suite of Wordpress plugins for various Bitcoin economic parameters: difficulty, # blocks, market cap, ticker.

That would be even more ideal hahah.

Unfortunately my code expertise does not lie with PHP, but rather .Net, so I'm not well suited to undertake such a task.

Perhaps someday I'll get around to learning the language and putting together a nice package like that.  It would be nice if some pro-php people got around to this though.  There's a lot of e-commerce and informational sites that could benefit from such a suite of Wordpress plugins.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: Clark on February 16, 2012, 04:58:02 PM
That would be even more ideal hahah.

Unfortunately my code expertise does not lie with PHP, but rather .Net, so I'm not well suited to undertake such a task.

Perhaps someday I'll get around to learning the language and putting together a nice package like that.  It would be nice if some pro-php people got around to this though.  There's a lot of e-commerce and informational sites that could benefit from such a suite of Wordpress plugins.

Should we set up a bounty?


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: finway on February 16, 2012, 05:01:20 PM
thanks.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: Raoul Duke on February 16, 2012, 05:09:16 PM
Plugin based widget for wordpress.
Just activate the plugin and you'll have the Bitcoin price Widget on your widget administration screen, ready to add to a sidebar of your choice.

It's a little bit rough still, with no options to edit, but I'll probably change that in a couple days. It only took like 5 minutes to put this together.

Download here:
Clicky Clicky (http://pmakordeoneditora.pt/wp-content/uploads/2012/02/bitcoinprice.zip)


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: Clark on February 16, 2012, 10:10:55 PM
Plugin based widget for wordpress.
Just activate the plugin and you'll have the Bitcoin price Widget on your widget administration screen, ready to add to a sidebar of your choice.

It's a little bit rough still, with no options to edit, but I'll probably change that in a couple days. It only took like 5 minutes to put this together.

Download here:
Clicky Clicky (http://pmakordeoneditora.pt/wp-content/uploads/2012/02/bitcoinprice.zip)

Would you look at that!


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: michaelsuede on February 17, 2012, 07:29:32 PM
Plugin based widget for wordpress.
Just activate the plugin and you'll have the Bitcoin price Widget on your widget administration screen, ready to add to a sidebar of your choice.

It's a little bit rough still, with no options to edit, but I'll probably change that in a couple days. It only took like 5 minutes to put this together.

Download here:
Clicky Clicky (http://pmakordeoneditora.pt/wp-content/uploads/2012/02/bitcoinprice.zip)

Thanks psy.

This widget is basically just a rehash of the code I presented at the top of the page though, only you can't edit the way the info is displayed without editing the file.  Not that I don't appreciate you putting it into a widget file format though.

It would be nice to see a widget that incorporates caching and short code tags for the display of variables.  Perhaps a customizable CSS as well.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: Raoul Duke on February 17, 2012, 07:55:27 PM
I just inserted your code in a plugin so it would be easier to add the widget and gave you credit for it.

For caching people should use http://wordpress.org/extend/plugins/wp-widget-cache/ which will add options to set a timed cache to each widget individually. WP Widget Cache is not to replace the WP-Cache or WP Super Cache, it’s a plus for them. In the case of a high traffic site that should be already covered but it's a good thing you brought the subject up to the conversation.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: film2240 on February 18, 2012, 01:14:01 AM
This doesn't work on wordpress.com sites but the only thing that shows is the BTC address and text message,the php widget does not show.Just for those who use wordpress.com blogs.Seems that wordpress.com doesn't allow anything other than basic html on their site.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: michaelsuede on February 20, 2012, 07:25:20 PM
This doesn't work on wordpress.com sites but the only thing that shows is the BTC address and text message,the php widget does not show.Just for those who use wordpress.com blogs.Seems that wordpress.com doesn't allow anything other than basic html on their site.

Yeah I was afraid of that.

Wordpress.com does block all scripting, to the point where you can't even embed video using standard embed code.  They will allow Youtube shortcode embeds, but that's about it.  The lack of script embedding on Wordpress.com is why I dumped that host for my own standalone in the first place.



Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: bitcoinspot.nl on March 06, 2012, 02:01:04 PM
does anyone know a ticker for the bitcoin prices in Euro?

Greets!


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: Raoul Duke on March 06, 2012, 03:26:54 PM
does anyone know a ticker for the bitcoin prices in Euro?

Greets!

Replace the URL with https://mtgox.com/api/0/data/ticker.php?Currency=EUR

Or just append
Code:
?Currency=EUR
to the one that's already there.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: bitcoinspot.nl on March 07, 2012, 07:41:16 AM
aah thanks :)


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: bitcoinpreneur on June 25, 2013, 11:43:58 PM
Thanks for all your early work in getting these widgets out there. We are in the process of releasing a suite of Bitcoin Plugins for WordPress and will definitely consider learnings from these earlier plugins in our best practices. Also just sent out a couple of tips :)


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: JessyMatt on January 25, 2014, 06:15:32 AM
Can anyone tell me how to display latest prices for the top currencies in that one script like USD, CAD, Pounds, UR without having to have many copies of the script?


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: Raoul Duke on January 31, 2014, 11:23:17 AM
Can anyone tell me how to display latest prices for the top currencies in that one script like USD, CAD, Pounds, UR without having to have many copies of the script?

I think you will need one curl call for each currency, which can get pretty taxing on your server if you get the tickers on each page load.
Unless you can find a ticker which displays all currencies using one call only, ofcourse.

EDIT: Found it: https://api.bitcoinaverage.com/ticker/global/all

Tell me which currencies you want and which value(s) to show for each currency and I'll see what I can do about it if you can't do it yourself.


Title: Re: Easy Wordpress Bitcoin Price Widget Using PHP Script
Post by: JessyMatt on March 05, 2014, 03:31:52 AM
Can anyone replace Mt Gox with another on this? Mt Gox is dead. Maybe Blockchain or something. I'm using this on my site http://www.bitcoinvalues.net but the prices are useless now cause Mt. Gox no longer exist.