Bitcoin Forum
April 24, 2014, 01:56:29 PM *
News: Due to the OpenSSL heartbleed bug, changing your forum password is recommended.
 
   Home   Help Search Donate Login Register  
Pages: [1] 2  All
  Print  
Author Topic: Easy Wordpress Bitcoin Price Widget Using PHP Script  (Read 4924 times)
michaelsuede
Jr. Member
*
Offline Offline

Activity: 38


View Profile

Ignore
February 15, 2012, 04:48:43 PM
 #1

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/

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 Wink ), visit my site:
http://www.libertariannews.org/

Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1398347789
Hero Member
*
Offline Offline

Posts: 1398347789

View Profile Personal Message (Offline)

Ignore
1398347789
Reply with quote  #2

1398347789
Report to moderator
1398347789
Hero Member
*
Offline Offline

Posts: 1398347789

View Profile Personal Message (Offline)

Ignore
1398347789
Reply with quote  #2

1398347789
Report to moderator
bitcoinspot.nl
Sr. Member
****
Offline Offline

Activity: 299



View Profile WWW

Ignore
February 15, 2012, 10:28:57 PM
 #2

Great,

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

cheers!

- bitcoinspot.nl - Alles over bitcoin! -
michaelsuede
Jr. Member
*
Offline Offline

Activity: 38


View Profile

Ignore
February 15, 2012, 10:48:23 PM
 #3

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.
Clark
Hero Member
*****
Offline Offline

Activity: 540


Lead Developer - ZeroBlock


View Profile WWW

Ignore
February 16, 2012, 07:06:40 AM
 #4

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.

ZeroBlock: The Bitcoin Trading Platform.
PGP KEY | 1Bitcoin3Tg2KWyAq3wzivdqwYqGwKYaGd
michaelsuede
Jr. Member
*
Offline Offline

Activity: 38


View Profile

Ignore
February 16, 2012, 03:31:09 PM
 #5

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.
Clark
Hero Member
*****
Offline Offline

Activity: 540


Lead Developer - ZeroBlock


View Profile WWW

Ignore
February 16, 2012, 04:24:10 PM
 #6

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.

ZeroBlock: The Bitcoin Trading Platform.
PGP KEY | 1Bitcoin3Tg2KWyAq3wzivdqwYqGwKYaGd
michaelsuede
Jr. Member
*
Offline Offline

Activity: 38


View Profile

Ignore
February 16, 2012, 04:38:04 PM
 #7

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.
Clark
Hero Member
*****
Offline Offline

Activity: 540


Lead Developer - ZeroBlock


View Profile WWW

Ignore
February 16, 2012, 04:58:02 PM
 #8

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?

ZeroBlock: The Bitcoin Trading Platform.
PGP KEY | 1Bitcoin3Tg2KWyAq3wzivdqwYqGwKYaGd
finway
Hero Member
*****
Offline Offline

Activity: 714


View Profile

Ignore
February 16, 2012, 05:01:20 PM
 #9

thanks.

Raoul Duke
aka psy
Global Moderator
Hero Member
*
Offline Offline

Activity: 1078


XBT.pt - BTC/DOGE


View Profile WWW

Ignore
February 16, 2012, 05:09:16 PM
 #10

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

Clark
Hero Member
*****
Offline Offline

Activity: 540


Lead Developer - ZeroBlock


View Profile WWW

Ignore
February 16, 2012, 10:10:55 PM
 #11

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

Would you look at that!

ZeroBlock: The Bitcoin Trading Platform.
PGP KEY | 1Bitcoin3Tg2KWyAq3wzivdqwYqGwKYaGd
michaelsuede
Jr. Member
*
Offline Offline

Activity: 38


View Profile

Ignore
February 17, 2012, 07:29:32 PM
 #12

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

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.
Raoul Duke
aka psy
Global Moderator
Hero Member
*
Offline Offline

Activity: 1078


XBT.pt - BTC/DOGE


View Profile WWW

Ignore
February 17, 2012, 07:55:27 PM
 #13

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.

fanquake
Donator
Sr. Member
*
Offline Offline

Activity: 378



View Profile

Ignore
February 18, 2012, 01:00:50 AM
 #14

Looks interesting. I might have to have a shot at putting something together.

Join #bitcoin-aus on Freenode
12eb1xeCnDE1F64vXkvoqq6MrAuSqaCBMG
film2240
Hero Member
*****
Offline Offline

Activity: 882


Professional filmmaker


View Profile WWW

Ignore
February 18, 2012, 01:14:01 AM
 #15

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.

[This signature is available for rent]
[This signature is available for rent]
[This signature is available for rent]
fanquake
Donator
Sr. Member
*
Offline Offline

Activity: 378



View Profile

Ignore
February 18, 2012, 02:13:40 AM
 #16

Thought I might put together a widget, you can display the price, and it has an option to display a donation address.

My address is in my sig if you wanna send come coin my way Wink



Code:
<?php

/*
Plugin Name: Bitcoin WordPress
Description: Add bitcoin stuff to your WordPress blog
Version: 1.0
Revision Date: February 18, 2012
Requires at least: WP 3.2.1, PHP 5.3
Tested up to: WP 3.4, PHP 5.3.6
*/

function getBitcoinPrice() {
 // 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']);

?>


<ul>
<li><strong>Last:</strong>&nbsp;&nbsp;<?php echo $mtgox_array['last'];   ?></li>
<li><strong>High:</strong>&nbsp;<?php echo $mtgox_array['high'];   ?></li>
<li><strong>Low:</strong>&nbsp;&nbsp;<?php echo $mtgox_array['low'];   ?></li>
<li><strong>Avg:</strong>&nbsp;&nbsp;&nbsp;<?php echo $mtgox_array['avg'];   ?></li>
<li><strong>Vol:</strong>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $mtgox_array['vol'];   ?></li>
</ul>
<?php
}
 
class bitcoin_widget extends WP_Widget {
 
// Constructor //
    
function bitcoin_widget() {
$widget_ops = array( 'classname' => 'bitcoin_widget''description' => 'Show some bitcoin stuff' ); // Widget Settings
$control_ops = array( 'id_base' => 'bitcoin_widget' ); // Widget Control Settings
$this->WP_Widget'bitcoin_widget''Bitcoins!'$widget_ops$control_ops ); // Create the widget
}

// Extract Args //

function widget($args$instance) {
extract$args );

$title  apply_filters('widget_title'$instance['title']); // The widget title
$show_price = isset($instance['show_price']) ? $instance['show_price'] : false// Show the Bitcoin price
$donate = isset($instance['donate_bitcoins']) ? $instance['donate_bitcoins'] : false// Get some bitcoins for your blog
$donation_address = isset($instance['donation_address']) ? $instance['donation_address'] : false// Donation address

// Before widget //

echo $before_widget;

// Title of widget //

if ( $title ) { echo $before_title $title $after_title; }

// Widget output //
?>



<?php 

if ($show_price){
getBitcoinPrice();
 }
if ($donate) { ?>

<p style="font-size:10px;">
Send me some Bitcoins! <?php echo $donation_address?>
</p>
<?php }

// After widget //

echo $after_widget;
}

// Update Settings //
 
function update($new_instance$old_instance) {
    
$instance['title']     = strip_tags($new_instance['title']);
$instance['show_price']     = $new_instance['show_price'];
$instance['donate_bitcoins']     = $new_instance['donate_bitcoins'];
$instance['donation_address']     = $new_instance['donation_address'];
return $instance;
}
 
// Widget Control Panel //

function form($instance) {

$defaults = array( 'title' => 'Bitcoins!''show_price' => 'on''donate_bitcoins' => 'on''donation_address' => 'xxx'  );
$instance wp_parse_args( (array) $instance$defaults ); ?>


<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>'" type="text" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('show_price'); ?>"><?php _e('Show the Bitcoin price?'); ?></label>
<input type="checkbox" class="checkbox" <?php checked$instance['show_price'], 'on' ); ?> id="<?php echo $this->get_field_id('show_price'); ?>" name="<?php echo $this->get_field_name('show_price'); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('donate_bitcoins'); ?>"><?php _e('Add a bitcoin donation address?'); ?></label>
<input type="checkbox" class="checkbox" <?php checked$instance['donate_bitcoins'], 'on' ); ?> id="<?php echo $this->get_field_id('donate_bitcoins'); ?>" name="<?php echo $this->get_field_name('donate_bitcoins'); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('donation_address'); ?>"><?php _e('Donation Address:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('donation_address'); ?>" name="<?php echo $this->get_field_name('donation_address'); ?>" type="text" value="<?php echo $instance['donation_address']; ?>" />
</p>
        <?php }
 
}

// End class bitcoin_widget
add_action('widgets_init'create_function('''return register_widget("bitcoin_widget");'));
?>

Join #bitcoin-aus on Freenode
12eb1xeCnDE1F64vXkvoqq6MrAuSqaCBMG
michaelsuede
Jr. Member
*
Offline Offline

Activity: 38


View Profile

Ignore
February 20, 2012, 07:25:20 PM
 #17

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.

bitcoinspot.nl
Sr. Member
****
Offline Offline

Activity: 299



View Profile WWW

Ignore
March 06, 2012, 02:01:04 PM
 #18

does anyone know a ticker for the bitcoin prices in Euro?

Greets!

- bitcoinspot.nl - Alles over bitcoin! -
Raoul Duke
aka psy
Global Moderator
Hero Member
*
Offline Offline

Activity: 1078


XBT.pt - BTC/DOGE


View Profile WWW

Ignore
March 06, 2012, 03:26:54 PM
 #19

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.

bitcoinspot.nl
Sr. Member
****
Offline Offline

Activity: 299



View Profile WWW

Ignore
March 07, 2012, 07:41:16 AM
 #20

aah thanks Smiley

- bitcoinspot.nl - Alles over bitcoin! -
Pages: [1] 2  All
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!