Functional Plugin VS Child Theme

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:

<?php
/**  * Plugin Name: My First Plugin  */
 
That’s it. You have your first valid plugin. It’s an empty plugin that does absolutely nothing, but it’s valid and you can activate it from the plugins page.
 
There are other comments that you can add to give more information about your plugin, read the codex to have more details.
 
After you have your functional plugin ready, you will be able to add there the code instead of popping the functions.php of your child theme.
 
Probably it’s a delusion for you, I’m sure you thought creating a plugin was harder than creating a child theme. Everybody suggests creating a child theme, and creating a functional plugin is even easier? I know you, you are thinking creating a child theme has more advantages, because everybody suggests doing it. Let’s see if it’s always true.
 
What’s the difference between writing code in the functions.php of the child theme or in a functional plugin? Is it the same in terms of performance?
Let’s answer the last question: no, no measurable differences in terms of performance. Even if you already have a child theme for other reasons, adding a new empty plugin will introduce no measurable performance loss. You can read this blog post to have an idea about how an empty plugin can slow down a website.
Loading a plugin means nothing else than calling its main file, as loading a child theme is nothing else than calling its functions.php. The functions.php of a child theme is in every respect a plugin, the only difference is that it loads after all plugins and before the functions.php of the parent theme (that you can also see as a plugin).
 
So, until now, creating a functional plugin is even easier than creating a child theme. Loading a functional plugin doesn’t slow down the website more than the functions.php of the child theme, but everybody suggests creating a child theme. Why?
 
You may add code for these two reasons:
– You want to customize the design and style of your website.
– You need additional functionality that is not provided by other plugins.
 

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.

You can even create a functional plugin using Freesoul Deactivate Plugins. read here to see how to do it with a couple of clicks.

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.