In the second and final part of this mini-series titled, "Collecting Donations With WordPress", you'll learn how to write a WordPress plugin which allows users to send you a donation via Bitcoin.
The plugin uses its own backend settings panel, and is highly customisable.
So, let's get started!
Initialising the Plugin
Step 1
In the wp-content/plugins
directory of your site, create a new folder called donate-bitcoins
.
Step 2
Now, within that folder, create a file called donate-bitcoins.php
.
Step 3
Finally, you need to add the Plugin Header information, which will tell WordPress that your new plugin actually exists on your server. You can change these details to whatever you wish, though they should typically be in that order with a minimum of that information.
<?php /* Plugin Name: Bitcoin Donate Plugin URI: http://code.tutsplus.com Description: Simple Bitcoin donation plugin. Version: 1.0.0 Author: Sam Berson Author URI: http://www.samberson.com/ */
Step 4
You'll now see your new plugin showing up in the Plugins page of the WordPress admin. Go ahead and Activate the plugin, though you won't see much happening just yet.
Adding the Shortcode
You'll be able your donate button by using a simple shortcode in any posts or pages you create. Essentially, a shortcode is a small piece of text, wrapped in square brackets, that allows you to call any function or action from a plugin or theme, in the post editor.
In this plugin, the shortcode will be, [donate]
, and this can be added anywhere in your posts or pages.
Step 1
To add the shortcode to WordPress, you need to use the add_shortcode
function, and within it, define what the shortcode will be (in this case, 'donate'), and then you'll define some of the option information. Since we'll be outputting HTML, we'll need to start tracking the output. You'll also need to close the PHP brackets before the next part.
function bitcoin_donate_shortcode() { $donate_options = get_option( 'bitcoin_donate_options' ); $address = $donate_options['bitcoin_address']; $counter = $donate_options['bitcoin_counter']; ob_start(); ?>
Step 2
Now, you'll call the CoinWidget script into the plugin, and define some JavaScript information. Then, reopen the PHP tag, capture the output, and close the function.
<script src="http://coinwidget.com/widget/coin.js"></script> <script> CoinWidgetCom.go({ wallet_address: '<?php echo $address; ?>', currency: 'bitcoin', counter: '<?php echo $counter; ?>', alignment: 'bl', qrcode: true, auto_show: false, lbl_button: '<?php _e( 'Donate', 'bitcoin_donate' ) ?>', lbl_address: '<?php _e( 'My Bitcoin Address:', 'bitcoin_donate' ) ?>', lbl_count: 'donations', lbl_amount: 'BTC' }); </script> <?php return ob_get_clean(); }
Bitcoin Wallet Information
You're now going to setup some of the information for the Settings form, which will allow you to setup your Bitcoin's wallet information.
Step 1
You can begin by defining a new function, called bitcoin_donate_wallet_address()
and by using the get_option()
function.
function bitcoin_donate_wallet_address() { $options = get_option( 'bitcoin_donate_options' ); echo "<input name='bitcoin_donate_options[bitcoin_address]' type='text' value='{$options['bitcoin_address']}'/>"; }
Step 2
Let's go ahead and add a new function, called bitcoin_donate_counter()
which defines the drop down options in the settings panel, which will allow you to set which of the following numbers to display next to the donate button: "Transaction Count", "Amount Received", or "Hidden".
function bitcoin_donate_counter() { $options = get_option( 'bitcoin_donate_options' ); ?> <p> <label> <input type='radio' name='bitcoin_donate_options[bitcoin_counter]' value="count" <?php checked( $options['bitcoin_counter'], 'count', true ); ?> /> <?php _e( 'Transaction Count', 'bitcoin_donate' ) ?> </label> </p> <p> <label> <input type='radio' name='bitcoin_donate_options[bitcoin_counter]' value= "amount" <?php checked( $options['bitcoin_counter'], 'amount', true ); ?> /> <?php _e( 'Amount Received', 'bitcoin_donate' ) ?> </label> </p> <p> <label> <input type='radio' name='bitcoin_donate_options[bitcoin_counter]' value= "hide" <?php checked( $options['bitcoin_counter'], 'hide', true ); ?> /> <?php _e( 'Hidden', 'bitcoin_donate' ) ?> </label> </p> <?php }
Step 3
You should now add an empty callback, which is needed to ensure that the plugin functions correctly. It's simply defining a new WordPress function, opening it, and then closing it again.
function bitcoin_donate_callback() { // Optional Callback. }
Hooking It All Up
Now you've generated your shortcode and form fields, you need to connect it back up to the WordPress admin, so that the plugin is functional.
Step 1
You should begin by registering the plugin's settings and fields with the backend, by adding the following code. Simply put, this code tells WordPress what to display in the admin.
function bitcoin_donate_register_settings_and_fields() { register_setting( 'bitcoin_donate_options', 'bitcoin_donate_options' ); add_settings_section( 'bitcoin_donate_settings_section', __( 'Main Settings', 'bitcoin_donate' ), 'bitcoin_donate_callback', __FILE__ ); add_settings_field( 'bitcoin_address', __( 'Bitcoin Address:', 'bitcoin_donate' ), 'bitcoin_donate_wallet_address', __FILE__, 'bitcoin_donate_settings_section' ); add_settings_field( 'bitcoin_counter', __( 'What should the counter show?', 'bitcoin_donate' ), 'bitcoin_donate_counter', __FILE__, 'bitcoin_donate_settings_section' ); } add_action( 'admin_init', 'bitcoin_donate_register_settings_and_fields' );
Step 2
You'll now tell WordPress what HTML to use when displaying the Settings form in the backend.
function bitcoin_donate_options_markup() { ?> <div class="wrap"> <h2><?php _e( 'Bitcoin Donate Options', 'bitcoin_donate' ) ?></h2> <form method="post" action="options.php" enctype="multipart/form-data"> <?php settings_fields( 'bitcoin_donate_options' ); do_settings_sections( __FILE__ ); ?> <p class="submit"> <input type="submit" class="button-primary" name="submit" value="<?php _e( 'Save Changes', 'bitcoin_donate' ) ?>"> </p> </form> </div> <?php }
Step 3
Finally, you'll tell WordPress what the Settings page is called, which user role can access it, and which HTML (defined above) to use for it.
function bitcoin_donate_initialize_options() { add_options_page( __( 'Bitcoin Donate Options', 'bitcoin_donate' ), __( 'Bitcoin Donate Options', 'bitcoin_donate' ), 'administrator', __FILE__, 'bitcoin_donate_options_markup' ); } add_action( 'admin_menu', 'bitcoin_donate_initialize_options' );
Final Source Code
Your plugin should now be fully functional, by adding the [donate]
shortcode to your posts or pages! Here's the full source code for the plugin:
<?php /* Plugin Name: Bitcoin Donate Plugin URI: http://code.tutsplus.com Description: Simple Bitcoin donation plugin. Version: 1.0.0 Author: Sam Berson Author URI: http://www.samberson.com/ */ function bitcoin_donate_shortcode() { $donate_options = get_option( 'bitcoin_donate_options' ); $address = $donate_options['bitcoin_address']; $counter = $donate_options['bitcoin_counter']; ob_start(); ?> <script src="http://coinwidget.com/widget/coin.js"></script> <script> CoinWidgetCom.go({ wallet_address: '<?php echo $address; ?>', currency: 'bitcoin', counter: '<?php echo $counter; ?>', alignment: 'bl', qrcode: true, auto_show: false, lbl_button: '<?php _e( 'Donate', 'bitcoin_donate' ) ?>', lbl_address: '<?php _e( 'My Bitcoin Address:', 'bitcoin_donate' ) ?>', lbl_count: 'donations', lbl_amount: 'BTC' }); </script> <?php return ob_get_clean(); } add_shortcode( 'donate', 'bitcoin_donate_shortcode'); function bitcoin_donate_wallet_address() { $options = get_option( 'bitcoin_donate_options' ); echo "<input name='bitcoin_donate_options[bitcoin_address]' type='text' value='{$options['bitcoin_address']}'/>"; } function bitcoin_donate_counter() { $options = get_option( 'bitcoin_donate_options' ); ?> <p> <label> <input type='radio' name='bitcoin_donate_options[bitcoin_counter]' value="count" <?php checked( $options['bitcoin_counter'], 'count', true ); ?> /> <?php _e( 'Transaction Count', 'bitcoin_donate' ) ?> </label> </p> <p> <label> <input type='radio' name='bitcoin_donate_options[bitcoin_counter]' value= "amount" <?php checked( $options['bitcoin_counter'], 'amount', true ); ?> /> <?php _e( 'Amount Received', 'bitcoin_donate' ) ?> </label> </p> <p> <label> <input type='radio' name='bitcoin_donate_options[bitcoin_counter]' value= "hide" <?php checked( $options['bitcoin_counter'], 'hide', true ); ?> /> <?php _e( 'Hidden', 'bitcoin_donate' ) ?> </label> </p> <?php } function bitcoin_donate_callback() { // Optional Callback. } function bitcoin_donate_register_settings_and_fields() { register_setting( 'bitcoin_donate_options', 'bitcoin_donate_options' ); add_settings_section( 'bitcoin_donate_settings_section', __( 'Main Settings', 'bitcoin_donate' ), 'bitcoin_donate_callback', __FILE__ ); add_settings_field( 'bitcoin_address', __( 'Bitcoin Address:', 'bitcoin_donate' ), 'bitcoin_donate_wallet_address', __FILE__, 'bitcoin_donate_settings_section' ); add_settings_field( 'bitcoin_counter', __( 'What should the counter show?', 'bitcoin_donate' ), 'bitcoin_donate_counter', __FILE__, 'bitcoin_donate_settings_section' ); } add_action( 'admin_init', 'bitcoin_donate_register_settings_and_fields' ); function bitcoin_donate_options_markup() { ?> <div class="wrap"> <h2><?php _e( 'Bitcoin Donate Options', 'bitcoin_donate' ) ?></h2> <form method="post" action="options.php" enctype="multipart/form-data"> <?php settings_fields( 'bitcoin_donate_options' ); do_settings_sections( __FILE__ ); ?> <p class="submit"> <input type="submit" class="button-primary" name="submit" value="<?php _e( 'Save Changes', 'bitcoin_donate' ) ?>"> </p> </form> </div> <?php } function bitcoin_donate_initialize_options() { add_options_page( __( 'Bitcoin Donate Options', 'bitcoin_donate' ), __( 'Bitcoin Donate Options', 'bitcoin_donate' ), 'administrator', __FILE__, 'bitcoin_donate_options_markup' ); } add_action( 'admin_menu', 'bitcoin_donate_initialize_options' ); function bitcoin_donate_translations() { load_plugin_textdomain( 'bitcoin_donate', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); } add_action('after_setup_theme', 'bitcoin_donate_translations');
In Summary
You've now learned how to develop another totally new plugin, which allows users to donate via Bitcoin. You can now initialize a plugin, use shortcodes, and add a settings page to your WordPress admin.
If you have any questions, please feel free to leave a comment below, and I'll be sure to help you out!
Comments