Creating a WordPress Plugin

If you have a WordPress website, you almost certainly know about the value of plugins. You might have a dozen or more of them installed on your site right now. And while you may know your way around the plugin block: which plugin is best for caching, security, contact forms, etc., you might not realize just how easy it is to create your own WordPress plugin.

I’m not talking about a full fledged plugin that extends your website like WooCommerce or BuddyPress or anything nearly as complicated. Those plugins contain several subdirectories, files, and thousands of lines of code. I’m talking about a simple plugin with a few useful functions that you can easily create yourself and easily install on new sites.

Easy? Really? Very easy. EZ PZ. So easy that if you haven’t done it yet for fear of the lack of skills, you’re going to laugh at how easy it is. Have you ever created a child theme and added a function to your child theme’s functions.php file? Then you can easily do this.

Just to prove how easy it is, I’ll show you how to create a plugin with just 4 lines of code:

That’s it. Granted, the plugin doesn’t do anything but it would still technically be a plugin. Now here’s how you package these four lines of code so you can install it as a WordPress plugin:

Create a new file on your computer and name it my-custom-plugin.php.

Copy the contents of the code above into the new file.

Zip the file and name it my-custom-plugin.zip.

Now log into your WordPress dashboard. Go to Plugins->Add New->Upload Plugin and install and activate the zip file you just created.

Congratulations!! You just created your first WordPress plugin and installed it on a site!! How easy was that?

Now what? We have a plugin that doesn’t do anything. It doesn’t even have a description in the dashboard. Whoopie!! What’s the big deal?

Here’s the big deal.

Remember earlier when I asked if you had ever added a function or code to a child theme’s functions.php file? There are plenty of tutorials online (this site included) that show you how to accomplish something using this method. Now you can place this code inside a plugin instead. Why?

Portability. Maybe you have a function that you like to add to all of your or your client’s websites. Instead of asking for FTP access to your client’s host, creating a child theme (if they don’t already have one), adding a functions.php file to the child theme and then adding your function – you could just install a plugin with that function. And since plugins can be installed right from the WordPress dashboard, all you need to do is log into the site as an admin. No FTP. No Cpanel.

Isolation. Anyone working with WordPress long enough should know the first step to troubleshooting any WordPress website error is to start by disabling plugins. Some of them are poorly coded. Some outdated. Some conflict with each other. Because the WordPress core is so stable, the problem is almost always a theme or plugin. If you put your custom function in a plugin, you are essentially isolating it.

Maybe you put all of your WooCommerce functions in a custom WooCommerce plugin. All of your BuddyPress custom functions in a custom BuddyPress plugin. And so on. Now if one of your functions causes problems, simply disable that plugin until you can find the solution. If it causes a big enough problem, WordPress might even disable it for you so it doesn’t break your site. There is no safeguard if you place your function in the functions.php file. Your website could completely break and require FTP or Cpanel access to fix it.

Upgrading. What happens to your custom functions if you put them in your functions.php file and change themes? Adios custom functions. You’ll have to remember to copy those functions to your new child theme. If you have them in a plugin, you don’t have to do anything. It will still be installed and activated when you switch themes. If the plugin was specific to the old theme and is no longer needed, you can easily delete it. This goes back to the Isolation point. If you group your functions together in separate plugins, you could delete the theme custom functions plugin when you change your theme but keep your WooCommerce custom functions plugin.

Now that we know how easy it is to create a WordPress Plugin, let’s add a description for it that will show up in the dashboard and actually do something so we know the functions work. Edit the my-custom-plugin.php file and add the following lines:

If you want to use this for a real plugin, feel free to replace everything with your information. Otherwise for example purposes you can just copy and paste. The information above will display on the plugins page where you activate/deactivate plugins.

Now let’s make our plugin do something. Add the following code to the same my-custom-plugin.php file:

The filter and function above will replace all instances of the word “kittens” with the word “puppies” in your post content. Not a super useful plugin but at least we can see some immediate results. Follow the same steps as we did with the original plugin. Zip the file and install and active it on your website.

Add the word “kittens” to one of your blog posts and then view that post. You should see “kittens” has been changed to “puppies”. If you don’t see that, make sure the plugin is activated. Once you’ve verified the filter is working, deactivate the plugin. You should see the word “kittens” again.

I realize this is about as basic of a WordPress plugin tutorial as it gets. My goal is to show you how easy it really is to create your own plugins. Of course the sky is the limit if you want to go on to create a complex plugin. But not all plugins need to be complex. You can reap the benefits of WordPress plugins with a single function like the example above.

If you want to try something more complex, you might want to take a look at my tutorial on creating a custom post type which is accomplished by creating a plugin.

 

Post a Reply

Your email address will not be published. Required fields are marked *