I’m about to share some information about creating custom post types, custom fields and custom taxonomies. Before I begin though I thought it would be good to lay down some foundations so you don’t find yourself having to do major work if something should change. If you want to save your self hours of work every time you switch to a new theme then this post will change your life. Okay, that was a little dramatic. 🙂
In The Old Days (Nothings Changed)
When I started modifying WordPress sites years ago it was all the craze to search the web for various functions to enhance your WordPress site. In fact, it still is. And to this day many tutorials still tell you to place these custom snippets of code into your themes functions.php file. But for the record, this is the wrong way to handle custom code on your site. Does it work, sure it does. But it also limits you and complicates your life in the futures should you want to make a change.
Bad Functions.php
You theme’s functions.php file is powerful and has a purpose. It stores all the functions that are particular to that specific theme and only that theme. Plugins should be used whenever you want to add functionality that should remain regardless of your current theme. The reason is simple. If you add this functionality to your theme and then down the road decide to use a different theme then you will lose all those custom functions unless you copy and paste them all over to your new theme. What a pain!
If you have them in a plugin (don’t be afraid, these are easy to create) then if you switch a theme you still have all those custom functions and you didn’t have to hunt to find them in some Frankenstein file of functions. Here are some examples.
- If you want a custom excerpt length because of how your theme displays excerpts, this should go into your themes functions.
- If you want to add a new custom post type that should be available regardless of which theme you are using, this is plugin territory.
- If you want to add custom CSS under certain conditions, this should go into your themes functions.
- If you want to add Google Analytics code to your site, this is plugin territory. No you shouldn’t put this in your header. That would be the same as placing it in your functions as it’s tied to your theme.
Why Isn’t This Mentioned More…
I have no idea. There could be any number of reasons why this important info is sometimes ignored but I think it’s because many who write about WordPress don’t know any better because…
- They’ve never created a plugin
- They think it’s difficult
- They don’t think they can explain it
- They don’t think their reader will understand it
But Here are the Facts
- If you are comfortable modifying your functions,php file you are fully capable of creating a simple plugin
- It’s extremely easy
- It can mostly be copy and pasted each time you want to make a new one
- Any code you’ve already pasted in your functions.php can most likely be used in a plugin
- You could do it today (or Friday if you are waiting for my post on how to create a plugin)
So those the guidelines. Stop adding code to your functions.php file UNLESS it’s meant only for the theme you are currently using. Otherwise, create a plugin.
Paul says
Hi
Nice article I understand what you are saying I’ve recently been converted into this mindset I used to overload my functions.php file with all sorts of useful snippets which do generic jobs. I’ve been slowly going through all these and converting them into plugins and it’s so much better. So much easier to manage the code and easy to turn this functionality on and off.
I’ve seen tutorials like using Google analytics without a plugin and now find them useless it’s much easier to make a plugin for this.
James says
Thanks, Paul. I agree. Easy to troubleshoot, disable, and modify. I’ve seen a lot of those “without a plugin” tutorials and they drive me crazy. It’s also funny that you mention Google Analytics because I wrote about that exact scenario today. http://wpninjas.com/tutorials/how-wordpress-plugins-work/
Thanks for commenting.
Paul says
Yea I’m guilty of those without a plugin tutorials, do a couple of them awhile ago.
Have learnt the ways of the plugins now and won’t be going back :).
Decowl says
Its true that its easy to maintain plugins than bunch of codes in functions.php . But the real problem comes when we are selling the themes, its more work/more steps for users who want to use the themes made by us which comes with a couple of plugins for them to install. So when it comes to themes that we use for our own purpose, plugins seems to be a better way. If you are selling themes or coding themes for others, i guess functions.php is lot more easier.
James says
I can understand where you are coming from as I have developed themes in the base and plan on doing so again soon. But I still stand on the fact that if the feature should be active regardless of the theme then it should be a plugin and not in the functions.php file.
I’m getting ready to role out a plugin theme package in the next couple months. The plugin will handle adding new features and options while the theme will only register options that are specific to the theme. I want my users to be able to change their theme freely without losing stuff that’s important to them.
Ashley Torres says
Hi there, hoping you can help… I am creating my own site using wordpress.org. I tried to add a code for an instagram badge and I added it to the wrong spot (themes) now I can’t login to the dashboard or my website. Please help! This is the error message im getting
Parse error: syntax error, unexpected ‘<', expecting ')' in /hermes/bosweb26d/b2647/ipg.ashleyssweetdesignsc/wp-content/themes/smallbiz/functions.php on line 759
James Laws says
The best solution is, using FTP, downloads that functions.php file and remove all that code you added and then upload the updated file to the same location. This should correct your issue.