Plugins have played such a crucial role in driving WordPress’ continuous success over the years that it’s impossible to imagine the platform without them.
However, while we’ve all probably popped the hood of a theme in order to make a few stylistic tweaks at some stage, the inner workings of plugins are still a mystery for many WordPress users. There remains a slight sense of uncertainty and unease among non-technical users about what exactly is going on behind the scenes. That’s a situation we’re setting out to shed some light on in our six-part guide to building a WordPress plugin from scratch.
In this introductory piece, we’ll briefly introduce some core plugin concepts, create a quick throwaway example, and set the stage for what our real plugin (a random quote generator) will deliver.
Let’s get some quick caveats out of the way before we crack on.
Who This Series Is Aimed At
As with our previous REST API tutorial, we’re tackling this series largely from the point of view of a non-technical WordPress user. If you’re an experienced PHP developer and/or plugin author, this is not the learning track for you. Your needs would probably be better served by diving into any of Josh Pollock’s recent pieces here on the site.
We’re targeting folks who have a bit of general WordPress experience under their belt, and are comfortable actually selecting and installing plugins, but who’ve never really gotten their hands dirty with the actual code involved. We’ll take things step by step, assume very little PHP or programming knowledge, and point you in the direction of relevant resources throughout.
In order to follow along, all you’ll need is a local copy of WordPress to experiment with. This, in itself, can represent a slight hurdle for some people, so we’re recommending the free version of DesktopServer. This should help keep things as simple as possible and avoid you getting tangled up in the weeds of local server configuration. Guides for setting up local WordPress installs from scratch are also available here on Torque if you want to go down that road, however.
You won’t magically be coding complex commercial plugins by the end of our series, but you will have a substantially better idea of what goes on behind the scenes of the average offering, along with a firm foundation for exploring further on your own.
Let’s move on to start looking at what we’re actually dealing with here in a little more depth.
The Basic Moving Parts of a Plugin
Although plugins vary considerably in terms of what they set out to achieve, they all share a certain basic structure and should operate within the parameters set out by the official WordPress Plugin Developer Handbook (an excellent resource that we’ll be referring to throughout the series).
The handbook defines plugins as follows:
Plugins are packages of code that extend the core functionality of WordPress. WordPress plugins are made up of PHP code and other assets. These include, but are not limited to, images, CSS, and JavaScript.
These packages of code live in the /wp-content/plugins directory of your site, and they interact with WordPress’ database, have their own admin settings and menus, make heavy use of actions and filters, and have to take factors such as internationalization and security into account.
A quick scan through the chapter headings of the Plugin Developer Handbook will reveal many of the other moving parts involved – don’t panic if it looks like there’s a terrifying amount going on at first glance; we’ll be hitting most of them along the way as we move through our series!
Rather than get bogged down in too much detail too early, however, let’s jump right in and make a very quick demo plugin just to show that there’s nothing really to be feared here.
Throwing Together a Quick Plugin
Working locally with DesktopServer, we’ve fired up a quick throwaway site called www.helloworld.dev to quickly kick the tires of this whole plugin idea. We’ll steal a trick from the folks at MainWP by quickly stepping through their guide to creating a bare bones WordPress plugin.
Popping into the /wp-content/plugins/ directory on our fresh install, there’s not a whole lot going on straight out of the gate. The Akismet plugin is housed within a folder of the same name, and Matt Mullenweg’s Hello Dolly plugin is lurking within the file hello.php.
We’ll create a new folder called /helloworld, and create a PHP file within that called helloworld.php with the following contents:
<?php /** * Plugin Name: Hello World Plugin * Plugin URI: http://www.torquemag.io * Description: This plugin is a quick test to see if we can create plugins. * Version: 1.0.0 * Author: Tom Ewer * Author URI: http://www.torquemag.io * License: GPL2 */ ?>
This is what’s known as the information header of the plugin and represents the bare minimum we need to actually create something that WordPress recognizes as a plugin. If we pop into Plugins in the back end, we’ll see our functionality-free plugin proudly displayed and waiting to be activated:
Let’s make sure our plugin actually does something when it’s switched on. Again, we’ll take a bit of basic code from MainWP’s article and stick it into our helloworld.php file:
add_action( 'wp_footer', 'my_function' ); function my_function() { echo 'hello world'; }
Even in this extremely stripped-down example, we’re glossing over a number of important concepts, so don’t worry too much about the ins and outs of what’s actually going on here. Basically, we just want to see if we can output something to the screen down in the footer.
If we now pop into the front end, we should see our text sitting pretty at the bottom of the screen:
We won’t be dwelling any further on this minor masterpiece, but hopefully it’s made one thing relatively clear: creating a plugin and affecting what appears on the screen is well within our technical grasp. We’ll be putting together a much more structured piece of work throughout the series, so let’s get a quick preview of what’s to come before we wrap up for today.
What We’re Going to Be Building in Our Series
In homage to Matt Mullenweg’s Hello Dolly, and to tie in with our recent REST API series, we will put together a random quote plugin over the course of upcoming articles.
We’ll kick things off by using a solid boilerplate framework to enforce some best practices which were merrily ignored in the illustrative sample above, and then move on to look at adding initial settings for users to play with. In subsequent pieces, we’ll gradually build out functionality using custom content types, and start integrating various display options such as templates, widgets, and shortcodes.
By the time we’ve wrapped things up, you should have a solid idea of what goes into producing a relatively straightforward plugin, and be in good shape for taking things forward from there with your own projects.
Conclusion
As one of the three core components of the WordPress platform, it’s well worth getting to know how plugins actually work as a site owner. There’s no better way of doing that than actually rolling up your sleeves and creating one from scratch, and that’s exactly what we’ll be doing over the rest of our series!
Having gently introduced some core concepts in this introductory piece – and rolled out a quick and dirty demo – we’ll be getting down to brass tacks the next time around with a more structured kickoff in our actual project as we start putting together our random quote plugin in earnest.
As always, we’re keen to get feedback throughout the series. If there’s an aspect of plugin development that’s been mystifying you to date, please feel free to get in touch via the comments below and let us know!
Featured image: Taken
No Comments