Table of Contents
This question was asked of us recently, after a tutorial we posted, so I’m putting this out there in case anyone else wants to know this – and also so that I can link this up in future WordPress tutorials too.
Where is functions.php in WordPress?
When developers talk about “adding this code to your functions.php file” or whatever, this is the functions.php file in your currently active theme.
This is located at: `[wordpress install location]/wp-content/themes/[your-active-theme]/functions.php`
Note – use a child theme for edits
Our Preference – Use a Plugin
We’ll stop short of calling this Best Practice, though there is certainly a “wrong” way to add to functions.php (that would be adding to a parent theme functions.php file on a theme which you got from an updated repository… when updates go live, you’ll lose your modifications).
For anything which adds a small change to your sites functionality, we prefer and recommend to do this using a plugin – either a custom plugin, or one which holds “snippets” of code. We like the plugin “Code Snippets” for this, because this allows you to name, and save, each of your snippets of code, and be selective about where these are triggered (for example, front end only, or only on admin).
If you do this, then you can turn off your snippets independently at the click of a button, in case there’s ever an issue, of if you’d just like to see how it looks “off” and “on”.
Are we saying “don’t use functions.php to store snippets?” – yes and no. Yes, we’re saying “try not to” but also, no, in development, its a legitimate place to put a snippet of code to test a feature. In the longer term, moving your snippets to their own plugin or – for micro stuff – into a snippets plugin with labelling will aid stability and maintainability for your WooCommerce store or WordPress website. It will be easier to isolate all your code, and to selectively add and remove elements.
A great big functions.php pile of code (even with good comments) makes life more difficult in the future – and probably there are few legitimate reasons for dumping code into here, bearing in mind how easy it is to create feature plugins, or load snippets in using a plugin like the above. Of course, there are performance ramifications to consider, too, but that’s a more advanced conversation than this article!
Did this help you?
Leave a comment below if this helped you to learn something new today… or particularly if you still have questions! Its how we are able to deliver better and better tutorials, by answering real questions, where there are documentation gaps out there.