You can’t call yourself a true WordPress expert until you’ve built at least one plugin of your own. Perhaps you want to distribute it for use on the WordPress plugin repository, or maybe you prefer an approach like hosting it on GitHub. Or you might even develop a plugin just for one site! Something that’s uniquely tailored to your needs and which you’ll never have to show another person.
Whatever your reasons, this tutorial will show you how to build your own plugin in WordPress and install it without any need for FTP. Bonus instructions at the end of the article for how you can submit it for distribution to WordPress.org!
Step 1: Create a Plugin Folder with a File
The first step is to choose a name for your plugin. This actually consists of two things:
- The actual title that you want others to see and which will show up in the plugins page
- The “slug” that’s like the URL slug for your posts
Take (b), and create a folder using the slug as the folder name. Inside that folder, create a file with the “PHP” extension – again using the slug as the name. For example, I’m going to create a plugin with the following:
- Plugin name: “My New Plugin Name”
- Slug: “new-plugin”
So here’s my new folder with the plugin file inside it:
Step 2: Add Basic Code to the PHP File
The file with the PHP extension is where you’ll place the PHP code for your plugin functionality. But before we start adding code, we need to add some plugin headers to give WordPress basic information about it. There are a lot of headers, but the only one that’s truly mandatory is the “Plugin Name”. Use the following code to get started:
<?php /* Plugin Name: My New Plugin Name Description: A New Plugin from Scratch! */
Note that I haven’t included the closing ?> PHP tag. This is by design. The best practice for WordPress PHP files is to leave out the closing PHP tag for the file. The reason is that it’s dangerous and can cause your site to crash if there’s even a single space or newline after it. And it’s almost impossible to debug!
After these comments, you can place your actual plugin PHP code.
Step 3: Zip the folder with the file
Using the regular “Send to zipped file” functionality of Windows. Right-click the folder, click “Send to” and choose “Compressed (zipped) folder” as shown here:
Make sure the name of the zipped file is the same as your plugin “slug” that we saw in Step 1.
Step 4: Upload the Plugin to WordPress
Now log into WordPress and go to “Plugins -> Add New”. Click “Choose File” and “Install Now” as shown here:
This will upload the zipped file and allow you to activate the plugin. After activation, you can see it in your Plugins dashboard like this:
You’re done! You’ve just built a plugin for WordPress! Remember to add your PHP code below the plugin header under the comment section – otherwise, it won’t do anything.
(Optional) Step 5: Prepare your File for Upload to WordPress.org
To upload your plugin to the WordPress repository, you need to do a few things:
- Add a readme.txt file
- Add some more details to the plugin headers
As for header requirements, the more information you can include in the plugin, the better. Here are a bunch of options from the WordPress documentation, as well as a sample comment header.
Hopefully this tutorial helped you to build your plugin – whether for personal, or public use.