How many times have you read “add this code in the functions.php of your theme”?.
And how many times have you read this?:
“Add this code in a functional plugin”.
I guess you have seen the first sentence all the times you were looking for a snippet for some functionality, and maybe never the second sentence. You may even don’t know what’s a functional plugin.
Does it mean you should always have a child theme for adding some code? And what’s a functional plugin?
First, a functional plugin is nothing else than an empty plugin that you then fill with your custom code.
Creating a functional plugin is very easy. Just add a subfolder in the plugins directory with the name of your plugin, and inside that folder add a PHP file with the same name. The name of the folder and the file must have only alphanumeric characters or the symbol -.
If for example, you want to create a plugin called “my-first-plugin”, add a subfolder called my-first-plugin under the folder plugins, and then a file called my-first-plugin.php inside that subfolder.
You will have something like [main-directory]/wp-content/plugins/my-first-plugin/my-first-plugin.php.
Then edit the file my-first-plugin.php and add this:
* Plugin Name: My First Plugin
Deciding between a functional plugin and a child theme is very easy:
- For custom design and style, add your code in the functions.php of your child theme.
- For custom functionality, add your code in a functional plugin.
If in the future you need the same functionality on a different website with a different design, it will be a lot easier to install the same functional plugin you used for that functionality.
If you need the same design and style, then you will install the same child theme.
Usually, if you make a new website, you will need a different design, but many times similar functionality, so it should be normal to use more times a functional plugin, right?
Moreover, because a functional plugin runs always before the theme and the child theme, some functions of the core and other plugins can be hooked with a functional plugin, but not with a child theme, because when some hooks fire, it may be too late for the code included in the functions.php.
So, in shorts, creating a functional plugin is even easier than creating a child theme, in terms of performance a functional plugin and a child theme are the same, usually, you need the same functionalities but different design on different websites, with a child theme you can’t do all that you can do with a functional plugin, but almost nobody suggests to create a functional plugin, and everybody suggests to create a child theme, always. No matter if the child theme stays empty for the rest of its life, people create a child theme. They have a page builder for the design, dozen of add-ons for the page builder, CSS Superman, or CSS Hero, I don’t remember how it calls, but they need to create a child theme because they have read that you need a child theme.
If for you creating a functional plugin is still something that is harder than creating a child theme, here on this website you will find the tool “Plugin Builder“. I have created it because I’m a fan of functional plugins. Now you have no excuses.
I was forgetting…, in any case, no matter if you create a functional plugin or a child theme, never edit the code of a plugin or the parent theme, in another case, you will lose your changes with the next update.