Are you a functions.php hoarder? If your first inclination when adding custom code snippets to your WordPress site is to stick them in functions.php, you may be afflicted with this terrible disease and not even know it.
Let’s be honest, most people add code snippets to their theme’s functions.php file. And I understand why - that’s how most guides tell you to do it.
But, it’s actually not a great idea to always use your functions.php. Creating a custom WordPress plugin is often a better solution for housing your added code snippets.
In this post, I’ll dig into when it’s ok to use functions.php and when you should use a plugin instead. Then, I’ll show you how to create your very own WordPress plugin to store your custom code snippets.
What is functions.php?
If you’ve ever poked around your WordPress folders, you might have noticed that your WordPress site includes multiple functions.php files. The functions.php which I’m talking about is the one in your theme’s folder.
Quite simply, it’s supposed to store all of the functions which are specific to your theme. If you pay close attention to my use of “specific”, you can probably follow where the rest of this post is going...
Why It’s a Bad Idea to Always Use functions.php
Let me start by saying that it’s not always bad to use your theme’s functions.php file to add code snippets. But it isn’t an all-purpose solution and you shouldn’t use it like that.
Let’s start with reason #1:
If you update your theme, you can lose all of your added functions. Now I already know what you’re shouting at your screen: “Colin! That’s why child themes were created”. And you’re totally right...child themes eliminate this issue.
But have you ever helped a moderately tech-savvy friend out with their WordPress site? I know plenty of people who are confident enough to add tweaks to their functions.php but not knowledgeable enough to follow child theme best practices.
That’s why it’s on the list - if you’re one of those people using your theme’s functions.php without a child theme - here’s your wake-up call.
Reason #2 - Even if you are properly using a child theme for functions.php, what happens if you ever need to change themes? I hope you documented every addition to your functions.php because without clear comments, it’s going to be a major pain moving everything to your new theme.
Reason #3 - If you ever add code to your functions.php which completely bricks your WordPress install (e.g. a white screen of death), you’ll need to FTP into your site, download functions.php, remove the offending portion, and reupload it. That’s annoying when there’s a better way to handle the scenario.
So When Should You Use functions.php?
Properly using functions.php in a child theme isn’t always a bad idea. Remember above when I told you to pay attention to “specific”? Here’s where that comes into play.
If you’re adding tweaks that are 100% specific to the theme you’re using, you can safely add them to functions.php (in a child theme!!). Here are a few examples of when this is the case:
- Adding a second stylesheet for your theme
- Changing the excerpt length to better suit that specific theme
- Adding fonts specific to that theme
- Adding a translation file
Basically, any time you’re adding, removing, or modifying something 100% specific to that theme, you can stick it in your child theme’s functions.php
...And When Is functions.php a Bad Idea?
It’s a bad idea to use functions.php when you’re likely to want the same functionality to carry over to a new theme. Here are some examples of when you wouldn’t want to use functions.php:
- Creating a custom widget that you’ll use over and over.
- Adding something theme-independent like Google Analytics tracking code
- Creating a custom shortcode
In such cases, it’s better to store those code snippets independently of your theme’s files. And you can do that with…a custom plugin!
Coding a plugin...that sounds daunting, right? Trust me, it’s not. If a dummy like me can figure it out, you can too!
How to Use a Custom Plugin Instead of functions.php
To create a plugin to house your custom code snippets, all you need to do is this:
- Create a .php file with some brief code that I supply
- Create a .zip file containing that .php file
- Install and activate the plugin like you would any other plugin
Yes, it really is that easy. Let’s get started.
Step 1: Create a PHP File and Add Code
To get started, you need to open a text editor like Notepad. Then, add this code to the blank document:
<?php /* Plugin Name: Colin Custom Functions */ /* Add Your Code Snippets Below This Line. Make Sure to Document What they Do With Comments Like This */ ?>
Of course, you can use your own name. In the end, it should look something like this:
Then, save the file as something like colin-custom-functions.php. You can name it whatever you want - just make sure that it’s unique so WordPress doesn’t confuse it with an existing plugin. I learned that the hard way when I named my first plugin my-custom-functions.php and it conflicted with the My Custom Functions plugin.
If you’re using Notepad, you need to select All Files from the dropdown so that you can save it as a .php file:
Step 2: Create a .zip File
Now, you just need to create a .zip file containing the .php file you created above. If you’re using Windows, here’s how you can do that:
Step 3: Install it Like Any Other Plugin
And that’s it! Now, you can upload the .zip file by going to Plugins → Add New → Upload Plugin:
Once you activate it, you should see it in your plugin list:
How to Add Your Code Snippets
To add code snippets, just stick them in the .php file you created. Or, you can always create a new plugin to separate different categories of snippets.
For example, here’s what it would look like if you wanted to create a “Hello World!” shortcode:
I also recommend that you create a comment above each code snippet. Comments don’t affect your code, but they do make it easier to remember what each code snippet does if you ever need to edit it at a later time.
To create a comment, just use this syntax: /* YOUR COMMENT */.
Then, you can either upload that file via FTP and overwrite the existing file. Or, you can delete your existing plugin and reinstall it with the new version.
Other Options to Avoid functions.php
Personally, I think that if you’re confident enough to add code to your functions.php, you should have no problem creating a custom plugin. It’s really not that difficult.
But, I understand if you don’t want to do things manually, so there’s actually a plugin called Code Snippets which can help you out.
It lets you add multiple code snippets, give them names and descriptions, and activate/deactivate each individual snippet as needed.
Another nice thing about the plugin is that it lets you import/export code snippets to other sites. So duplicating similar functions across all of your sites is fairly trivial.
It may seem like a small thing, but placing your code snippets in the proper locations will make it much easier to switch themes in the future. Beyond that, it’s simpler to deactivate a site-breaking plugin than it is to dig into your functions.php via FTP.
Do you have any questions about creating a custom WordPress plugin? Let me know in the comments and I’ll try to help.