Why You Shouldn’t Use functions.php (And What You Should Do Instead)

Why You Shouldn’t Use functions.php (And What You Should Do Instead)

I believe people who use WordPress can be separated into three broad camps:

  1. Plug and players: WordPress enthusiasts who are probably not experienced website designers and are looking for a turnkey solution. They will find a theme, install it, and probably get on with content creation.
  2. Tweakers: These folks will take a theme that could probably be used straight out of the box and apply their own tweaks. They will probably use the custom.css and custom_functions.php files (or similar) to customize their theme. They will take an off the shelf option and mess around with it until they have something that (a) suits their needs, and (b) looks unique.
  3. Frameworkers: These are usually developers who will start off with a theme framework that offers very little beyond – you guessed it – a framework, upon which content can sit. Hours or days later, they will have crafted crafted something truly unique and customized to their – or their client’s ≠ specific needs.

If I had to guess, I would say the biggest camp out of the three is the tweakers.

And let’s face it: WordPress is a tweaker’s paradise. Armed with nothing more than Google search and your functions.php file, wordPress makes it easy to add diverse functionality to your website with relative ease.

The only problem is many tweakers are playing with fire. Using functions.php to add functionality to your site can be an accident waiting to happen (for multiple reasons). In this article I will explain why, and offer you a far better alternative.

PHP
Add functionality to your WordPress site, but not with your functions.php file.

How WordPress Works

At the core of WordPress lies a simple principle: design and functionality should (whenever possible) be clearly separated.

That is why we have themes and plugins; ostensibly, themes are solely responsible for design and plugins are solely responsible for functionality. One should be able to switch themes without affecting functionality, and one should be able to deactivate plugins without affecting design.

This isn’t always possible (some of the reasons for which I’ll get onto shortly), but the separation of design and functionality is an ideal that any WordPress developer worth his or her salt should aim for.

So Where Does functions.php Fit Into This?

functions.php is a theme-specific file, by which I mean it exists in a WordPress theme’s directory and pertains only to that theme.

As you might guess from the name, functions.php contains functions that are relevant to the theme, examples of which are:

  • Use of featured images
  • Length of excerpts
  • Widgetized areas

These types of functions are exceptions that prove WordPress’ design/functionality separation rule. While they are elements of functionality, they are relevant only to their parent theme, and may not be appropriate when applied to another theme. As such, they belong in functions.php and should remain there.

For example, an excerpt length of 200 words might look fantastic on one theme but terrible on another. Although we are technically playing around with the “functionality” of a site by adjusting the excerpt length, including the necessary code in functions.php is the right thing to do.

I don’t intend to get into the complexities of effectively utilizing functions.php in this article, but it is worth mentioning at this time that if you’re going to include a lot of theme-specific functionality in your site, you should consider creating an inc directory within your theme (or using a pre-existing directory if it already exists) to effect some organization amongst your theme’s functions. Here’s a great read from WordPress developer Tom McFarlin on the subject if you want to go further down that path.

My ultimate point is this: functions.php is not the enemy. It is a valuable cog in a theme’s engine; necessary for all but the most basic of designs.

However, the misuse of functions.php is rife and should be stopped.

When You Shouldn’t Use functions.php

The simple answer to when you should use functions.php is “almost never.”

The slightly more complicated answer is “use your noggin.”

If you understand what functions.php is for (functionality strictly related to a particular theme), you should be able to figure out what it isn’t for.

Easy examples can be found within your WordPress website’s plugins directory. Plugins are (ideally speaking) logically arranged “packets” of code separated by their intended functionality.

One of the best (and higher profile) examples of the whole point of separating functionality from design in WordPress is Google Analytics tracking code, which looks like this:

Google Analytics tracking code
This code needs to be added into the head section of your site in order for visits to be tracked.

You have three main options when it comes to adding this code:

  1. Add it manually to your theme’s header.php file
  2. Add it manually to your functions.php (using a hook)
  3. Use a plugin

If you use options one or two, your tracking code will disappear if you change themes and visitors will no longer be counted by Google. (It may also disappear the next time your theme is updated.) But if you use option three (my recommendation is Google Analytics by Yoast), you’re golden. Unless you have a senior moment and deactivate the plugin you’re using, that tracking code is going nowhere.

My simple advise is this: If in doubt, use a plugin. They’re popular for a reason.

Why You Shouldn’t Use functions.php

In explaining the “when” above, I have also gone a fair way in explaining the “why:” Don’t use functions.php because changing your theme will change the functionality of your site, which is all kinds of wrong.

However, there are a couple of other major points I should make to further strengthen the reasons for avoiding functions.php as much as possible.

First of all, there is the dreaded “white screen of death.” Quite frankly, if you’ve never encountered this while messing around with WordPress, you haven’t lived. Or at least you were far more careful than me when you first started tinkering with WordPress.

I feel at this stage we need a screenshot to clarify what this white screen of death actually looks like:

White screen of death

Scary. But what does it mean?

Well, to put it in the Queen’s English, one encounters the white screen of death when one cocks up one’s PHP.

In other words, you’ve screwed up.

To put it another way, syntax errors in PHP code inserted into functions.php can “break” your site, leading to you seeing nothing more than a white screen. (Don’t ask me where the “death” comes into it. As far as I’m aware, nobody gets hurt.)

Upon encountering the white screen of death, it’s better if you were editing your site via FTP or an online IDE (like ShiftEdit) rather than the WordPress backend, otherwise you’ll have some trouble getting back into your site. Plus, you’ll get a slap on the wrist from me for using the WordPress backend. Naughty.

But I digress. The point I’m getting to is this: placing your PHP code into a plugin can prevent the white screen of death from ever happening. Instead, WordPress may be able to deactivate the offending plugin without negatively affecting the continuing operation of the site as a whole.

Secondly, using functions.php is just plain messy if you plan on inserting more than a handful of code; especially if the theme you’re using already has existing functions.

Thirdly, and as I have already mentioned above, code in functions.php is theme-specific (i.e. will not travel when you change themes) and can be overwritten by theme updates.

None of this is good, and should convince you that functions.php is not the place you want to place your code in the majority of cases. Sure; leave whatever’s there by default as it is (on the assumption that the theme developer has included theme-specific functionality only), but any new code you’re adding is likely fair game.

So what do you do instead?

The Beautiful Alternative: Create Your Own Plugin

If you’re a garden variety WordPress tweaker and have never created your own plugin before, you’d be forgiven for thinking that it involves some kind of complicated and arcane process. Perhaps some kind of incantation is required.

Fortunately, that’s not the case. If you know how to edit a functions.php file properly (via FTP or an online IDE), you already have the necessary skills to create a plugin for your all of the code you would otherwise be putting into functions.php.

So let’s do it.

Firstly, navigate to your WordPress website’s root folder via FTP (or your hosting panel’s file manager, or via an online IDE). From there, you should navigate to wp-content/plugins:

WordPress Directory

The folders you see within the plugins directory are (as you may imagine) the plugins currently installed on your site. To create your own plugin, just create a new folder. I’m going to name it tme-custom-functions:

Plugins list

(“TME” are my initials, and I’m prepending them to the plugin name so that I don’t accidentally use an existing plugin’s name.)

Next, create a PHP file with the same name within your plugin’s folder:

Custom Functions plugin

Finally, insert the following code into your brand new PHP file:

<?php

/**
* Plugin Name: Custom Functions Plugin
* Description: This plugin contains all of my awesome custom functions.
* Author: Tom Ewer
* Version: 0.1
*/

/* Your code goes below here. */

/* Your code goes above here. */

?>

That’s all there is to it! Make sure your file is saved and the plugin will now appear within your WordPress plugins list:

Plugins List

Of course, your plugin doesn’t do anything yet, but you are now free to insert any custom functions that previously resided in your functions.php file, and they will continue to work as normal.

Going a Step Further: Creating a “Must Use” Plugin

The average plugin is all well and good, but what if you want your chosen functions to work on your site under all circumstances? What if you don’t want your new custom plugin cluttering up your plugins list?

It sounds like you need to make it a “must use” plugin, which are plugins that are always active on your site (and cannot be deactivated). They won’t even appear on the plugins list in WordPress unless you click on the Must-Use link within the plugins list.

To make your plugin “must use,” simply create a folder named mu-plugins within your WordPress site’s wp-content directory, then move your plugin’s PHP file (not the whole folder) into it.

Hey presto! The plugin disappears from your list and reappears within a new Must-Use section:

Must Use

Wrapping Up

You now know when you should use functions.php and when you should create your own plugin.

You also know that it is almost always worth creating your own plugin for a number of reasons. Now you too can feel like a brainy WordPress developer by creating something that appears on the WordPress backend! Don’t pretend that it doesn’t make you feel far better at coding than you actually are (at least, that’s how it makes me feel).

If you have any questions or concerns about anything I’ve discussed in this article, please fire away in the comments section below and I’ll get back to you!

Photo Credit: skoop.