State of the file
There’s a wide spread digital myth in the WordPress microverse about the
functions.php file and that it’s a code snippet container. Let’s take a look at the opinions of some developers who think that this is plain wrong.
Konstantin Kovshenin has posted an article, which can be summed up that using this file instead of plugins makes code harder to maintain, port, share and debug. Then Rodolfo Buiaz posted a meme on G+ that had one interesting line:
Hello Dolly?! One line in functions.php IS A PLUGIN!
And he’s quite right with that. The
functions.php is what one could define as a Themeplugin container. Or at least it now mainly is used as such.
Let’s now take a look at what purpose it originally served:
- Holds all custom Template Tags your theme defines
- Loads the theme textdomain and translation files
- Loads additional theme files that the author uses for code organization (menus, etc.)
- Has filter or action callbacks that modify the internal behavior of WordPress core
- Defines custom backgrounds, Header images
- Registers option pages or Theme Customizer settings
And it still serves those purposes. So, as you can see, there’s already a lot going on in the
Why are you still misusing it?
As we already discussed the cons of stuffing snippets, we should take a look at the pros. The only pro that I’ve ever heard of was that it might be easier to add something to the
functions.php than to add a new plugin. This might be the case, if you are using the built in WordPress theme editor instead of a desktop texteditor. At a first glance, this might be convenient, but it only makes one thing easier: Adding code. When you finally want to switch your theme, you’ll have a hard time, as you need to port all your code snippets over to the new theme and test each if it works or if it collides with some part of your new theme. And then there’s this thing that you might have forgotten what part of the
functions.php was something that you added and what part was already in the theme when you installed it. And that’s a pain in the
Alternatives? A nano scale tutorial.
As in nearly every case in every situation in every part of the world, there’re alternatives that will help you to get most of both sides. In this case, you’ll even get everything of both sides. And this in less than five minutes.
- Open your texteditor. Something like Notepad++ or Proton is fine enough.
- Add a new file named
functions.phpand save it to your desktop.
Now add a plugin header comment on top. We’ll go with the minimum, the
Plugin Name:part. The following lines only do three things: 1) Open the PHP file 2) Deny direct file access for security 3) Give the plugin a name.
1. <?php 2. defined( 'ABSPATH' ) OR exit; 3. /** Plugin Name: Custom code snippets container */
Now use a zip program like Winrar, Winzip or 7zip, make an archive and go to your admin/plugins page.
Choose upload, select the file on your computer and upload your new plugin.
From the beginning to the end you shouldn’t have needed more than some minutes to complete this tutorial. You now have officially created your first plugin. So don’t be afraid, it doesn’t slow down your system – that’s just part of the myth. It’s absolutely equal in loading speed and execution time as if you’d still stuff snippets in your
What’s next? Now open your plugin file in your WordPress admin UI plugins editor. Then start moving all your custom snippets from your Themes
functions.php to your new Plugin
functions.php file. And you’re done! Congratulations! You just saved yourself some hours when switching themes, have a secure place for your snippets as they can’t get lost during Theme updates and you have even secured a file against direct file access (if this was possible before).
Now that you’re beyond level 1 of professional WordPress usage, you might consider taking a step further. There’re several routes that you can walk from here on. Each one will make your life even more easy, as you will be left with the option to activate or deactivate single snippets in case you get the ☠ white screen of death – or a nasty error message in case you got
WP_DEBUG set to
true in your
wp-config.php file more about debugging here.
- Use a plugin to manage your snippets. Code snippets by Shea Bunge – with a dedicated site for the plugin – and Toolbox by Sergey Müller are two easy to use solutions.
- Start moving your snippets into separate plugins. No, this won’t add any overhead or decrease performance.
…or move straight to the pro level:
- (S)FTP to your server and navigate to the
- Add a new folder named
What have you got now? The answer is simple, but pretty unknown: A M(ust) U(se) plugins container. To shed some light on this, here’s a short explanation what will happen and how this might help you. You’ll get an additional tab on the admin UI plugins page. It will appear after you (S)FTP-ed your first plugin there. Then you’ll notice that you can’t activate or deactivate any plugins in there. WordPress activates them automagically for your. And if you own and run a multisite setup, it will even activate them on every single site in your network.
There’s only one minor drawback to this solution: You’ll have to use your (S)FTP browser as there’s no upload interface for such plugins. But I guess you can live with that.
What is a plugin?
One thing that sometimes comes up in discussions about plugins vs.
functions.php as plugin container: “Is this plugin material?”. In any case the answer is simple and always the same: Yes. Look at the following example that deactivates the admin toolbar. In fact it’s just a single line of code. And it belongs in a plugin file. Why? Easy: Because it does add functionality and not style. If you need to add style, go with a Child Theme.
defined( 'ABSPATH' ) OR exit;
/** Plugin Name: Disable the admin bar */
add_filter( 'show_admin_bar', '__return_false' );
We’ve now discussed all the pros and cons of different solutions, have stepped into several levels of snippet handling and left you back with a bunch of options. Now only one question remains:
Are you going to use it?