Collecting Donations With WordPress: Bitcoin

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.

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.

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.

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.

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".

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.

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.

Step 2

You'll now tell WordPress what HTML to use when displaying the Settings form in the backend.

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.

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:

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!

Tags:

Comments

Related Articles