How (and When) to Create a Simple WordPress Plugin

0 Shares

Building a plugin isn’t that scary. It’s a lot simpler than you think and can save you tons of work down the line!

Do you love to tinker? Making tweaks and changes to your WordPress website can be really fun and rewarding. There’s just something about stepping back after working on a website to see the changes you’ve made come to life. It’s not without its pain points though; it can be hard work! One technique to make life a little easier is knowing how (and when) to create a simple WordPress plugin for the changes you make to live in.

How often have you built out a feature for a site, then had to rebuild it from scratch when you swapped themes?

Made a bundle of changes, realized a few days later something was broken, and had to spend hours troubleshooting to find the problematic code?

Developed a site for a client, had another client request a feature from that site, and had to spend forever digging through functions.php to find it?

If any of this is you, or even if you’re just curious, learning how to create a basic WordPress plugin is a great idea!

When should new changes go into a plugin rather than just be edited in?

Putting code into its own plugin rather than letting it exist as a functions.php edit has a few huge advantages:

  1. You can save a lot of time debugging your site when something breaks
  2. Functionality changes to your site can be easily retained when swapping themes
  3. Features you’ve built can be easily passed to other sites when needed

CSS, stylistic and layout changes to a site, and theme-specific changes in general can happily be left in your child theme’s functions.php. Non-theme specific functionality you’ve built is the perfect candidate for its own plugin.

By making a separate plugin for each of those types of changes, you make your site’s feature set compartmentalized and portable. Quickly select individual features you like to implement on another site, disable for troubleshooting, etc. No sweat.

Let’s also quickly bust a common myth here before we move on. The number of plugins on your site alone isn’t going to drag performance down. Bad quality plugins can, even just one. It’s not a numbers game, it’s a quality of code issue. Your site can be highly performant with lots of plugins so long as the code they contain is well written.

How to create a WordPress plugin in 3 simple steps

Creating a plugin sounds daunting to most anyone who hasn’t done it before. We’re not talking the ultra complex plugins like many we use every day, though. We’re not recreating Yoast, EDD, or Ninja Forms here.

If you can edit a functions.php file in the first place, you can build a plugin no problem!

1. Create a .php file

Bust open your text editor of choice, choose a name relevant to the functionality of the code you’re about to add, and save it as a .php file.

This file will need to live in your wp-content/plugins directory, preferably in a folder that shares a name with your file.

My example plugin will contain code that triggers a redirect, so I’ll run with this naming convention:

  • Folder: custom-redirect
  • File: custom-redirect.php

2. Add a header to the new file

There are way more things that could be added to a plugin file than we want to get into here. Let’s focus on the minimum viable data that’s required for a functioning WordPress plugin. That’s a brief header and your actual code.

While there’s also a lot of info that can be packed into a header, the minimum viable is as follows:

Just copy and paste that into your file, then change the actual plugin name to the one you want. Save and continue 🙂

3. Copy/paste your code into the file

Finish out your new plugin by adding the functional code snippet you want beneath the header.

I’ll add to mine a snippet that essentially allows me to add a piece of custom post meta to any page called “redirect” with a URL. When the page is called, if it contains this custom post meta then it’s automatically redirected to the new URL.

This is what it looks like with header and code pulled together:

What’s left to do from here? Activate your new plugin!

Work smarter, not harder. Building a WordPress plugin is simple and saves you time + energy!

Never spend hours troubleshooting individual edits to functions.php again. Always have individual features packaged and ready to be ported over any time you swap themes or build a new site. There’s no reason not to, so what are you waiting on? Create more, and share! There’s always room on WordPress.org 🙂

Share below too! What neat feature ideas have you put into a plugin lately?

0 Shares