For those of you who haven’t noticed, the latest version of the Nuts and Bolts Media website launched a little over a week ago. (Or did as of June 2014 when this was written.) Since all our service offerings have changed, I decided it was time for a new look as well.
One of the worst parts of changing your site’s design is maintaining the functionality of your old theme. If your old theme used shortcodes, for example, you’ll probably want to include the same shortcodes in your new theme to avoid a bunch of weirdness in your pages and posts. But hunting down each of those items and figuring out how to include them in a different theme can be a total pain in the butt – I speak from experience! This is why it makes sense to create a custom functionality plugin for your site.
What is a custom functionality plugin?
If you use the Genesis framework, you’re probably intimately familiar with the use of functions and other code snippets to change things on your site. (This isn’t unique to Genesis by any means, but other themes and frameworks may not work exactly the same way.) If you want to add a widget area, change the wording of your comment form, add or remove post info, or any number of other tasks, you are likely editing your Genesis child theme’s functions.php file. Which is fabulous — until you want to switch to a different child theme or create a custom one.
A custom functionality plugin is simply a different way to store functions for your site. By putting them in a plugin instead of functions.php, you can change child themes without losing those pieces of code that make your site work the way you want. Editing functions.php is not a bad thing (assuming you are using a child theme, that is) but a plugin is just a better way of accomplishing the same goal.
When should I use a custom functionality plugin?
Basically, if a function is tied to the specific theme you’re using, it should go in functions.php. If it needs to stay on your site regardless of the theme, it should go in a plugin.
Things that might belong in a plugin
- A logo for the login page
- Changes to the comment form
- Page-specific sidebars
- Custom footer credits
- Anything that improves your general site and not your theme
Things that might NOT belong in a plugin
- Theme-specific widget areas
- Functions to reposition navigation
- Functions to add/remove layouts
- Featured image sizes
- Custom header sizing
- Enqueued scripts just for your child theme
All that said, you may prefer to leave your functions.php untouched, and while there is no reason to do that (again, assuming you are using a child theme), a plugin is a good alternative.
Bonus! Ever have one of those moments when you’ve edited functions.php and your whole site breaks because of an error? By using a plugin, your site will keep working even if you make a mistake. If the plugin’s code causes a fatal error, the plugin will deactivate automatically and won’t reactivate until you’ve resolved the problem.
But too many plugins are bad!
Yes, we’ve been conditioned to believe that plugins are horrible. But when a plugin only contains functions, it is absolutely no different than adding the same snippet to functions.php.
Run a speed test on any page of this site and you should see load times under 1 second, assuming you’re in the US. Would you believe there are 36 active plugins as of this writing?
Out of all those plugins, 21 are functionality-related. They don’t load a bunch of scripts and stylesheets, thus allowing me to do the things I want without slowing down my site. It’s not the number of plugins that counts – it’s how they’re coded and what they do.
A real life example
As I mentioned, I recently built a new custom theme for the Nuts and Bolts website. In doing so, I had to be very careful to keep the functionality I wanted without bringing over anything I didn’t want or need.
Luckily, since I use a custom functionality plugin, I didn’t have to do anything other than alter the plugin’s stylesheet to make each element match the new color scheme. (You can put the CSS in your child theme’s stylesheet, but I prefer keeping it in the plugin to make things easy to find and tweak.)
Here are all the customizations stored in my own functionality plugin:
1. Enqueue scripts. There are a couple of scripts I use for all my sites, such as FontAwesome, so I enqueue them within the plugin.
2. Add a logo to my login page. Pretty self-explanatory. I want my logo on the login page instead of the default WordPress logo, so I changed it. If you’d like to do that on your own site, see this tutorial.
When changing child themes, be sure there’s a login-logo.png in your new theme’s /images/ directory or this will stop working. You could also put the logo image in a directory within the plugin, but I didn’t do that because it’s going to have to be swapped out either way.
3. Customize the filler text on search forms. If you look at the top of my sidebar, you’ll see that my search form says “Search Nuts and Bolts Media” instead of the typical Genesis “Search This Site.”
4. Force layout on blog posts, archives, and search results. Since this is a business website, my default layout is set to full width, but I want a sidebar on certain parts, such as blog posts. This will always be true no matter what new theme I build for the site.
5. Use a custom avatar for comments. Instead of the goofy default avatar, I set a custom one to show when a commenter doesn’t have a Gravatar. Want to do the same? Check out this tutorial from @cdils.
6. Add Google+ comments before regular comments. I am a big fan of G+ and wanted to allow readers to comment on posts from their G+ accounts, but I didn’t want to remove WP comments completely. This article will show you how to do the same on your own site. (LOL from 2021 at Google+ being a thing.)
7. Various ad-related widget areas. I don’t display a ton of ads on this site, but I do have a few that I don’t want to lose when/if I redesign the site. Those widget areas are hooked and registered in my plugin to make sure they transfer over.
8. Related posts. I used a tutorial from Nick the Geek to code my own related posts, so of course I want to keep that regardless of the theme I’m using.
How to make your own custom functionality plugin
Creating a functionality plugin is surprisingly easy. Open a text editor and paste in the following, swapping out my information for your own:
Once you’ve done that, you can start pasting in your functions. Be careful not to include any opening PHP tags when you’re pasting snippets from other sites or tutorials, though you do need the PHP tag in the header of your file as shown above.
Save the file as functionality-plugin.php (or whatever you’d like), then create an empty folder on your computer named functionality-plugin (or whatever corresponds with the filename) and stick the file in there. Zip the folder and you’re ready to upload it to your WordPress site using the plugin uploader.
Need to add something to your plugin? You can now open it in Plugins > Editor within your dashboard, though I do not recommend editing anything other than a stylesheet within WordPress. Instead, get a good FTP client (I love Transmit for Mac) and you can pop in whenever you’d like to add, change, or remove functions.
Other uses for custom functionality plugins
While the plugin I’ve outlined here is a general catch-all for theme functions, sometimes it makes sense to use a custom plugin for one specific purpose.
For example, one of the designers I work with needed an easy way to add a banner to a to a client site, but only temporarily. (The ad was supposed to stay live for three weeks.) So I created a plugin called “Mid Post Ad” with the banner code and the appropriate Genesis hooks. Once the two weeks ended, the client simply deleted the plugin and no one had to edit any code.
Another example is this code from Brady Vercher that caches post thumbnails on archive pages. I could add that code to my general plugin and it would work fine, but I chose to leave it separate in case I ever have a problem with my thumbnails and need to troubleshoot.
More recently, a partner needed to maintain about 10 shortcodes that were hard-coded into a client’s old theme. So we made a plugin, included all the relevant styles and scripts, and the transition to the new theme was seamless – no abandoned or missing shortcodes hanging out in the client’s posts.
Sometimes a custom functionality plugin just makes life easier, whether you’re a dev, a designer, or just a blogger who wants to make some changes. If you customize websites on a regular basis, this definitely needs to be part of your repertoire.
Do you use a custom functionality plugin for your websites? What questions do you have about using a plugin versus editing functions.php?