It’s a battle of the ages! In the red corner, weighing in at exactly one file: The ultra slim, light weight, nimble as a butterfly, custom plugin!
In the blue corner, weighing in at a hefty two files and extra dependency: The glamorous, expandable, child theme!
Who will win this match for title belt of WordPress custom functionality?
If you are a WordPress developer then you know the need for custom functionality is pretty much a constant. I do a ton of work in WooCommerce and small pieces of code often do the simple jobs I need far easier and less cumbersome then full blown plugins.
The question is, where do we put this code? There’s a million ways to skin this cat, but I believe only two that are proper.
DING! DING! DING!
Let’s talk about how WordPress handles this code for a minute. Typically, this custom functionality is going to be thrown into hooks. So when WP goes to execute the code in your theme’s functions or your custom plugin it will queue up the functionality where you tell it to.
So what’s the difference in using a plugin or a theme’s functions? Well, plugins are loaded right before the theme. So if you need that order for anything, you can rest assured the plugins are coming first.
AND HERE COMES CUSTOM PLUGIN WITH A LEFT HOOK
But what if you need a plugin to be available? You’d think you’d need to do this in the theme functions then. No sir, just hook into plugins_loaded.
AND THE UPPER CUT
You bet your ass it is. Plugins are far more modular. What if you want to move that functionality to a new site? Or you want to change to a new theme? Well, you have to go through the entire process of making a child theme again and moving the code over.
THE CHILD THEME IS REALLY TAKING A BEATING HERE, LADIES AND GENTLEMEN
If it’s in a custom plugin you can change themes without worry. Or just move the plugin to the new site and activate.
All WordPress requires for a plugin to be loaded is a file in the plugins folder with a plugin name in it. For example, if you create a file called my-custom-plugin.php and add it to the wp-content/plugins/ directory with the following code, you will see it as able to be activated in your admin plugins.
Now you can throw anything you want into that file and it will be executed with every page load.
EIGHT! NINE! TEN! AND CUSTOM PLUGIN TAKES THE TITLE BELT!
I personally believe, and this is my sole opinion, that using a custom plugin is ALWAYS the best choice for custom functionality and code snippets. I can’t think of one reason using a child theme would be a better solution. Child themes are for theming, that’s it. You need custom page templates and so on, then yes, use a child theme. If you need functionality that’s simply to be used by your child theme, then yes, go with a child theme. But those use cases are strictly related to the template.