In the second and final part of this mini-series, "Collecting Donations with WordPress," you'll learn how to write a WordPress plugin that allows users to send you donations via Bitcoin.
The plugin uses its own backend settings panel and is highly customizable.
So, let’s get started!
In your website’s wp-content/plugins
directory, create a new folder called donate-bitcoins
.
Now, create a file called donate-bitcoins.php
in that folder.
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 anything you want, but they should generally be in that order and contain minimal information.
<?php /* Plugin Name: Bitcoin Donate Plugin URI: https://code.tutsplus.com Description: Simple Bitcoin donation plugin. Version: 1.0.0 Author: Sam Berson Author URI: http://www.samberson.com/ */
You will now see the new plugin displayed in the Plugins page of your WordPress admin. Go ahead and activate the plugin, although you won't see much happening yet.
You can use the donate button in any post or page you create using a simple shortcode. Essentially, a shortcode is a small piece of text, enclosed 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 is [donate]
and can be added anywhere in your post or page.
To add a shortcode to WordPress you need to use the add_shortcode
function and define the shortcode there (in this case "Donate") and then you will define some option information. Since we will be outputting HTML, we need to start tracking the output. You also need to close PHP brackets before the next section.
function bitcoin_donate_shortcode() { $donate_options = get_option( 'bitcoin_donate_options' ); $address = $donate_options['bitcoin_address']; $counter = $donate_options['bitcoin_counter']; ob_start(); ?>
Now, you will call the CoinWidget script in 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(); }
You will now set up some information for the Setup form, which will allow you to set up your Bitcoin wallet information.
You can first define a new function called bitcoin_donate_wallet_address()
and use 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']}'/>"; }
Let's go ahead and add a new function called bitcoin_donate_counter()
, which defines the drop-down options in the settings panel, allowing you to set which digital donation buttons are displayed next to: "Transaction Count", "Receive to the amount" or "hide".
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 }
You should now add an empty callback, this is required to ensure the plugin functions properly. It just defines a new WordPress function, turns it on, and then turns it off again.
function bitcoin_donate_callback() { // Optional Callback. }
Now that you have generated the shortcode and form fields, you need to connect them back to your WordPress admin for the plugin to function properly.
You should first register the plugin's settings and fields with the backend by adding the following code. In a nutshell, 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' );
Now you will tell WordPress what HTML to use when displaying the Settings form on 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 }
Finally, you will tell WordPress what the Settings page is called, which user role can access it, and what HTML (as defined above) to use.
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' );
By adding the [donate]
shortcode to your post or page, your plugin should now work! Here is the complete source code of the plugin:
Copy after login
You have now learned how to develop another brand new plugin that allows users to donate via Bitcoin. You can now initialize the plugin, use shortcodes, and add a settings page to your WordPress admin.
If you have any questions, please feel free to leave a message below and I will definitely help you!
The above is the detailed content of Using WordPress to collect donations: Bitcoin. For more information, please follow other related articles on the PHP Chinese website!