how to build a quick ‘n’ dirty WordPress plugin

Plug it in by Abekamal on deviantART

Let’s say that you’ve in the process of writing a new e-book and you want to give the opportunity to your blog readers to submit their details in a form so they can receive a notification when the e-book is ready. You need a plugin. Ok, there are a few mailing list plugins out there that you can use quickly, but the point of this article is to show you how you can build such a plugin from scratch so you can modify it in any way you want, etc. You get the idea, so let’s start.

Have a look at the demo page (opens in a new tab). You can also download the tutorial’s source code.

First, make a new .php file, naming it whatever you want. I named mine “quickndirty.php” (like the one you downloaded). The first lines (the comments) are just for informative reasons (the info displayed in the “Plugins” section of your WordPress administration panel):

The whole plugin consists of one class, named “Qnd” (Quick’n’dirty). All the functions (unless stated so) are going to be included in this class. We declare 2 private variables, one to hold the database table name (make sure to use a unique name to avoid any conflicts with other tables) and one to hold an instance of wpdb:

The first function we are going to create is the one that will be executed when the plugin is activated and it’s going to be responsible to create the table into the database. The table is going to be created only if it is not present and it will not be deleted when the plugin is deactivated. The table consists of 4 (self-explanatory) fields:

Next is the controller function. It’s functionality is quite straightforward: If a POST request have been made try to insert the details into the database and if not, display the form:

We’ll first explore the displayForm function. It’s really just a simple form. The $error variable is used to check if there is an error and if there is to display a message.

submitDetails is the last function of Qnd class. First, it escapes the user’s input to prevent SQL injection. Then it checks if either the name or the e-mail fields are blank. If any of them is, it displays the form with an error message. Please note that in a proper implementation it’s recommended to use better error reporting, to check if the e-mail is valid, etc. Finally, if the required fields are there, it inserts the data into the database and displays a success message instead of the form.

The plugin is almost ready. Place the following code outside of the Qnd class and you’re ready. The code creates an instance of the Qnd class, registers the shortcode “QUICKNDIRTY” to be used to call the plugin and sets the function createTable to be executed every time the plugin is activated from the WordPress administration panel.

To use the plugin, place the .php file into your plugins folder, activate it in the administration panel, create (or edit) a post/page and insert the shortcode QUICKNDIRTY in brackets ([]).

Have a look at the demo page (opens in a new tab). You can also download the tutorial’s source code.

Photo by ~abekamal

  • Nerdy Goodness

    Fantastic article, thank you. I’m in the progress of building my first WP Plugin at the moment and I’ve found this invaluable.

  • Stathis

    You’re welcome :)

  • Amereservant

    One suggestion I will make is NOT creating a new table for plugins unless absolutely necessary. That was something I had to learn myself, but through following some great WordPress developers and most of them being part of the core WP dev team, I’ve learned a better way.

    Between using custom post types or users with custom roles, then post meta for the additional fields that those don’t cover, you’ll find yourself saving tons of custom code. On top of that, WordPress has some really nice built-in functions that do a lot of the heavy lifting such as JOINS etc. and cleanup is much simpler.

    For example, in your QND plugin, you could easily add them as new users with a custom role called “ebook_subscriber”, then add a user post meta for the location and everything else is handled internally by WordPress.

    I hope that helps and learning how to utilise this will save you time and make the WP gurus smile when they read your code ….. or just save you time.

  • Stathis

    @Amereservant: Thanks for the suggestion and the insight. Some links with more details on the subject could be useful as well.

    To tell you the truth, I prefer to design my own database schemas, and to write my own SQL queries. Even if I have to write more code, I find it quite useful and way more flexible when I later have to connect what I’ve build with other systems outside WordPress (or any other similar solution).

    But your point is valid :)