How to create a custom WordPress Plugin – Step by Step for Beginners (With Pictures & Code)
Many developers and digital marketers love WordPress, and with good reason. It’s simple to use, free (or open-source), versatile, and, best of all, expandable. You may extend the WordPress plugin to add almost any functionality you want.
WordPress plugins are similar to the apps on your phone if you’re a complete beginner. Your phone already has a lot of capability, but you can expand it with apps. The WordPress content management system follows the same pattern (CMS).
Although the WordPress platform is extremely powerful, plugins allow you to do even more. WordPress plugins, for example, allow you to add features like e-commerce, SEO, spam prevention, sophisticated forms, social media, improved security, live chat, visual page development, and more.
There are thousands of free and premium WordPress plugins available. At the time of writing, the official WordPress plugin repository alone has almost 59,000 free plugins! Thousands of premium WordPress plugins are available on other marketplaces, such as CodeCanyon.
That means you can probably find a plugin for almost any feature you want to add to your WordPress site. Even yet, there may be situations when you require a plugin that is not available from the aforementioned plugin sources. In that instance, you may need to start from scratch or adapt (or fork) an existing WordPress plugin.
And in today’s post, we’ll show you how to construct a simple WordPress plugin, which is useful if you want to start small or go right into plugin development. Let us begin with that preface because there is a lot to cover.
Writing a Simple WordPress Plugin: The Basics
Before we get into the fun part, there are a few things you should be aware of. As a newbie, you may believe that writing WordPress plugins is a difficult task. No, not at all. Whether or not you have a computer science degree, you can learn how to develop a plugin.
Simultaneously, if you wish to build more plugins in the future, some PHP coding skills will come in handy. Because WordPress plugins are written in the PHP computer language, this is the case. You will have a greater understanding of how PHP functions work as a result of this information, which will make your work much easier. Still, it’s a piece of cake.
Don’t worry, we’re not going to get into the technical details today. I’ll show you how to create a small plugin and provide you with a few tools and resources to make your job easier. Have you started to become excited yet? I certainly hope so.
What You Need to Write a WordPress Plugin
Before you start building your WordPress plugin, you’ll need a few items in place. To begin, avoid testing your plugin on your live site. If something goes wrong, you risk crashing your website, resulting in lengthy downtime while you fix it.
Instead, construct local or server-based testing or staging site. Here’s how you can do it:
- WAMP is a tool that allows you to install WordPress locally on Windows.
- MAMP allows you to install WordPress locally on your Mac.
You can also use services like Local by Flywheel or DevKinsta.
A text editor, such as Notepad++, SublimeText, or Atom, is required for the rest. While it isn’t required, you can speed up development by using tools like Pluginplate or Plugin Boilerplate (more about this later).
Let’s get to the fun part: writing a simple WordPress plugin. For this lesson, I’m going to make a plugin that will add a custom post type to a hypothetical restaurant website I’m working on.
WordPress provides a variety of post types, including page, post, attachment, revision, and navigation menu for the uninitiated. We’ll make a simple plugin to add the “Recipe” custom post type in our case scenario. For the sake of example, I’ll title my plugin “Hot Recipes.”
Let’s get started.
How to Write a Simple WordPress Plugin
Go to Pluginplate.com and click the Create Your Plugin button, which we’ll show you how to do below.
Creating the Main File
Then, as indicated below, fill out the plugin details. The Modules area, located near the bottom of the page, allows you to extend the functionality of your plugin. Also, as we’ll explain below, you can change each module by clicking the Plus (+) button. Click the Generate Plugin button when you’re finished:
After that, go ahead and click the Download button to store the plugin to your computer.
We now have all of the necessary basic files, including the main file. But hold off on popping the bubbly just yet; our plugin isn’t ready yet. The code that will be executed when the plugin is activated must be added. My major file, based on our example, is hot-recipes.php, which we will change in the next part.
Adding Extra Features
Extract the ZIP folder you downloaded from Pluginplate to find the hot-recipes.php mail file:
You should see your primary file inside the folder, which in our instance is hot-recipes.php:
There are a few more files inside the plugin folder, but we don’t need them right now. Let’s go ahead and add some functions to the main file now. In your favorite text editor (I use Notepad++), open the main file (hot-recipes.php).
Depending on how you filled out the form at Pluginplate, you’ll see the following code or something similar:
Everything appears to be in good working order. The above code informs WordPress of the plugin’s name, as well as its version, author, license, and other information. Nothing has to be changed. Let’s go on to the next phase.
Add the following code just below the above code:
The preceding code simply registers an array of options for our own post-type “recipes.” It also includes features like revision support, custom fields, excerpts, comments, and a prominent image, among others. When you add a new recipe, you’ll see these options in the post editor.
Aside: As a novice, I wouldn’t worry too much about syntax at this stage because it’s outside the topic of today’s post. However, if you know PHP, you’ll be able to decipher every bit of the above code and what it accomplishes. Besides, there are many resources and codes to study and practice on the internet.
Compress Your Plugin Folder
All of your modifications will be saved. Compress the hot-recipes folder (where you located the hot-recipes.php main file we just updated) into a hot-recipes.zip archive (on a Mac, right-click, compress file – and I guess it’s similar on a PC). If your folder does not save as a.ZIP extension, the plugin will not install.
After that, go to Plugins > Add New on your test site, as seen below.
After that, select the Upload Plugin button, navigate to your plugin ZIP folder on your computer, and then click Install Now:
Next, Activate the plugin:
If you go to the WordPress admin menu now, you’ll see our new Recipes post type, which includes the option to upload new recipes:
Congratulations on creating your first plugin! After reading this, you can experiment with the code to see what you can come up with. You can also learn more by studying the source code of other plugins (all WordPress plugins are open-source).
All you need now are a few more resources (check out the Plugin Handbook) and a lot of practice, and you’ll be coding like a pro in no time.