How to Safely Add Code to your Theme’s Functions File

If you’ve ever spent some time looking desperately through Google to find a tweak, customization, or modification to get just right, you’ve undoubtedly ran across an article or thread where the answer was to add a function. These are brief snippets of PHP code that modify output – I even mention functions in a lot of my articles, and they can look something like this:

What does that all mean, though? In this article, we’ll briefly cover what the functions.php file in your theme does, how to use it, and what to do when playing with it drags your site down to hell (it happens).

Update: December 29, 2016 There is an alternate way to add code than adding it directly to your functions.php sheet – see the guide here for more information. It’s not the ideal long-term replacement though, so the below method is still preferred.

What is the Theme Functions file?

In every single (properly coded) WordPress theme, you’ll find a few recurring files – one of which is functions.php. You can find it on your own WordPress site by accessing it through FTP and opening your theme there, or by going to Appearance > Editor and selecting the file from the sidebar:

Locating functions.php

Yours will look different – everything from your list of files on the right to the content in the white box. It all depends on what theme you’re using, but for sample’s sake, let’s use the Canvas Theme from WooThemes from here on out, with no Child Theme (tsk tsk).

What does functions.php do?

Your Theme Functions file (functions.php) contains PHP code in the form of Actions and Filters, all using Hooks. Without the technical description, it allows you to add, remove, or modify core WordPress code in your site. This can have a small impact like removing the date from your blog post descriptions or adding entire new sections to your site.

Most themes will move a lot of the heavy lifting to different template files, but taking our Canvas example into consideration there are some important code snippets there:

Canvas is particularly lightweight, and as WooThemes loves to do, is extremely well marked. We’ll come back to that in a moment, but let’s take a sidebar to break apart what functions.php needs to actually work.

Parts of functions.php

There are three important parts of the file we need to look at and understand. While some themes handle this well, not knowing what these lines do could lead you to delete them and break everything (see “going to hell” comment above).

The Opening PHP Tag

Every PHP file starts with <?php. That’s really all there is to it – this tells the browser to read the code as PHP, and if it’s missing, then nothing following it will work. It’s always the first thing to come, and should sit on its own line for clarity.

The Closing PHP Tag

Just like the beginning, there needs to be an ending. The closing PHP tag is ?>, and will be the last thing in your functions.php file. Again, leave it on the last line, by itself, unmodified.

Notation

Like most coding languages, PHP can have notes added for developers to quickly see what a section does, to help navigate, or in the case of Canvas, to tell you where to modify. Without getting into too much detail, there are two ways to mark a line of code as a note – either prefixing it with // or /*. This makes it so the browser ignores that line and moves to the next. Neither is better than the other, though it’s good practice to use them consistently – WooThemes uses /* for section headers and // for individual lines. /* tags will also need a closing */.

Something to remember is that it only applies to that line. Whereas in CSS we can wrap entire segments in a tag to have the browser ignore them, that’s not the case here:

Even though Line 9 is surrounded by commented text, it doesn’t have a prefix. Best case it just outputs that code somewhere you don’t want it – worst case it white screens your site, locks you out of the admin area, steals your credit card, gets a ton of debt, then flies off to the Bahamas.

How do I add code to my Theme Functions file?

Now that we have all the explanation out of the way, let’s get to it! Referencing the Canvas functions file above, scrolling down we can clearly see that the developers have marked off a good place for us to edit. Many themes will not be so clear – in that case, the best place to put your code is on the last possible line, above the closing ?>.

A great resource to start tweaking Canvas is the 65 Tips, Tricks, and CSS Tweaks for WooThemes Canvas found on pootlepress.com – it’s got some neat ones, but specifically let’s look at how to add a Login/Logout to our Primary Navigation for our example.

Here’s the code that the article provides us:

With that in hand, we’ll add it to our Theme Functions in the marked area:

You’ll note that I added a commented line before the function – this lets me go back and easily see what it does if I ever need to remove it.

When we refresh the page, we’ll have a shiny new feature added to our site. Break out the champagne!

What if something goes wrong?

Oh, something WILL go wrong if you play with the file enough. That’s why it’s incredibly important to BACK UP YOUR functions.php FILE BEFORE YOU MAKE ANY CHANGES. Copy them into a text editor offline where they’re safe, because if you didn’t catch it before, I mentioned that it’s very easy for this to lock you out of your site entirely. No frontend access, no backend access, and lots of panic.

If a change to functions.php breaks your site…

Then luckily, you have FTP access and a backup! To fix the issue:

  1. Log in to your site via FTP and navigate to wp-content/themes/[yourtheme]/functions.php
  2. Open functions.php in a text editor.
  3. At this point, if you know exactly what you copied in you can delete it manually. Alternatively, you can just delete everything and paste in your backup.
  4. Save your modified file.

Refreshing your site should get you back to where you were before – safe and sound.

Final Notes

As a last thing to remember, functions.php is more or less meant for little tweaks here and there. Typically, anything that involves major functionality or feature additions should be done through a plugin, which is an article for another day.

Hopefully this article has shed some light on your theme functions file. All said and done, it’s not something to be afraid of, but a healthy dose of respect and caution whenever making a change is a good move – just be sure to have backups before you do anything and everything. 🙂


Have any questions or comments about this article, or ways you think it can be improved?
Join the conversation in the comments below, or sign up for my newsletter to recieve periodic updates!

Tags: , , , ,

No comments yet.

Leave a Reply

%d bloggers like this: