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.
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.