2014-10-24

In the first 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 PayPal. 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 paypal-donations.

Step 2

Now, within that folder, create a file called paypal-donations.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 the form fields for the donate button, and the image information for the button.

Step 2

At this stage, you can add the new shortcode to a post or page, though it won't look quite right, and will likely throw up a few errors onto your site.

Callbacks & Form Functions

You'll now define the callbacks required to make the plugin work, as well as the form for the settings panel in the WordPress admin.

Step 1

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.

Step 2

Next, you're going to add a function which generates and input field in the admin settings form for your PayPal email address.

Step 3

Now, you'll generate an input field for the admin settings with radio buttons, so you can select which find of button you'd like - more on that later.

Step 4

Finally, we'll generate another input field with lots of drop down items, so you can select the currency in which your PayPal donations will be processed, by adding a PHP array.

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

Let's begin by registering all the settings, and their fields with WordPress, and then adding the action to the admin.

Step 2

You're now going to generate the HTML of the main options page in WordPress, by setting up a div with the class of wrap, and then opening up the form and importing the settings fields.

Step 3

Next you'll add the plugin's settings page into the WordPress admin, by using the options_init() function, along with the add_options_page function.

Step 4

The very last step, is to add the activation hook, and to check if the settings already exist. If they do, great - if not, the plugin will register the defaults.

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 learnt how to develop a totally new plugin, which allows users to donate via PayPal. You can now initialise a plugin, use shortcodes, and add a settings page to your WordPress admin.

In the next - and final - part of this mini series, you'll learn how to write a similar plugin, allowing users to donate using Bitcoins, instead of a conventional currency with PayPal.

If you have any questions, please feel free to leave a comment below, and I'll be sure to help you out!

Show more