If you’ve spent anytime as a WordPress theme developer, you’ll be intimintaly familiar with the functions.php file. It’s included in every theme, and is the main file for defining the functions which affect the behavior of your site/theme.
However, in many cases, your themes functions.php is not the best place to place your snippits!
Themes are all about presentation. That is, they define how your site looks and feels. It defines where to place things, and how to display them. That means enqueuing stylesheets or scripts, enabling theme support, defining widget areas, image sizes, etc, are all the responsibility of the theme.
When you need a plugin
When you start getting into defining logic, as oppose to presentation, you’ve reached plugin territorry. While many plugins do include CSS, and JS that are required for reasonable out-of-the-box default presentation, plugins aren’t really concerned with how your site looks. The primary purpose is to do something, rather than display it. Some of the things that would be included in a site-specific plugin:
- Registering custom post types
- Registering custom taxonomies
- Creating / saving metaboxes
- Creating custom functionality, like custom widgets
- Defining custom shortcodes
These are all things that should be theme agnostic. By that I mean, all of this type of functionality should operate independently of the theme.
More than a few times we’ve cloned a clients existing site to begin a new project, and we’re left with a mess of un-renderable shortcodes and post types that now have to be found, and cleaned up from the old theme.
But what if I’m not going to change my theme?
If you truly aren’t ever going to change your theme, than you have nothing to worry about. Go ahead and stuff all of that functionality inside your theme’s functions.php file, but…
You’re creating an unecessary limitation if/when you do decide that it’s time for a facelift. And if you’re doing any type of client work where you are handing off the product to someone else, you’ve created that same limitation for them!
Creating a site-specific plugin
Luckily, it’s not actually any more difficult to create a site specific plugin than it is to add code to your theme’s functions.php file. Granted, if you want to build something more complex than a few site-specific snippits, you’re probably going to want to use something more sophisticated, such as the popular WordPress Plugin Boilerplate.
Let’s take a look at a plugin that registers a post type of “Testimonials”:
As you can see, creating a plugin isn’t significantly different than creating a theme’s functions.php file. In this example, we are doing one thing: registering a custom post type. In the future, when it comes time to rebuild the website using this plugin, we can retain the testimonial functionality without an additional effort on our part.