Site icon Hip-Hop Website Design and Development

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

Have you come across WordPress tutorials asking you to add code to your functions.php file to get something done? Don’t do it! Read below and find out why…

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

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

In this article, I will explain why you shouldn’t add code to your functions.php file, and offer you some far better alternatives:

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

PHP vs functions.php

WordPress beginners often get confused and ask questions like:

First off, PHP is found everywhere in WordPress, as it’s the main language WordPress is built on. We’re not covering what PHP does in WordPress in this post, but you can read all about it in our Learn PHP tutorial series for WordPress development beginners.

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. Every theme should have a functions.php file located inside the theme folder.

A functions.php file is required for each WordPress theme and can be found inside the theme directory.

You can learn more about the functions.php file, what it does, and how to add functions in our ultimate guide to the WordPress functions.php file.

So Where Does functions.php Fit Into This?

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

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:

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 (I recommend using WPMU DEV’s own Google Analytics +), you’re golden. Unless you have a senior moment and deactivate the plugin you’re using, that tracking code is going nowhere.

My simple advice 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:

Yep… the WordPress ‘White Screen of Death’ displays a completely blank screen.

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?

What To Do Instead

Well, as it turns out, you have 3 options:

  1. Add it to the functions.php of a child theme
  2. Use a WordPress Custom Functions plugin
  3. Create your own plugin

Let’s take a brief look at each of these options:

Use Child Theme functions.php

You should always use child themes with WordPress. Especially if you plan to make any changes to your theme. This way, you won’t lose any custom design or modifications when the main (i.e. parent) theme of your site gets updated.

If you want to pull in styling from your parent theme to your child theme, for example, you will have to enqueue it in your child theme’s functions.php file. If your child theme doesn’t have one, you will need to create a functions.php file for it.

If you must use the rod, don’t spare the child theme functions.php

If you need to add code to the functions.php file, do it in your child theme’s functions.php.

Note, however, that this is not the recommended solution and it can lead to errors. There is a better alternative… use a plugin!

Use A WordPress Plugin For Functions.php

A better option than adding code directly to the functions.php file is to use a WordPress custom functions plugin.

My Custom Functions WordPress Plugin

My Custom Functions is a nifty free WordPress functionality plugin that lets you easily add custom PHP code to your WordPress website without editing the functions.php file of your theme or any of your plugin files.

To install the plugin, go to Plugins > Add New in your dashboard menu, type in “my custom functions” into the keyword search field, locate the ‘My Custom Functions’ plugin by Space X-Chimp, and click on Install Now, then Activate

Install and activate My Custom Functions WordPress plugin.

Once the plugin has been activated a new menu item appears in the WordPress Settings menu. To add custom functions to WordPress, select PHP Inserter from the Settings menu.

My Custom Functions WordPress menu

This brings up the My Custom Functions screen. Just add your custom PHP code into the field, click Save and turn the function ON and the plugin will automatically add your PHP code to your website without changing any of your theme files and without slowing down your website.

Add custom functions to WordPress with My Custom Functions plugin.

This plugin works independently of the theme you have installed, so you can use it with any theme, even if the theme does not have a functions.php file.

The benefit of using this method is that any code you add to your WordPress site using this plugin will remain unaffected if you edit the content of the theme’s functions.php file, or even if you change your theme. This also works for plugins, so your custom PHP code won’t be overwritten even if you update or switch themes or plugins.

There is one more option for adding code to your WordPress site without touching your theme’s functions.php file.

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

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:

WordPress custom functions plugin folder

(“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:

Use this WordPress custom functions plugin to add code to functions.php

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 your new WordPress custom functions plugin will now appear in your WordPress plugins list:

Your new WordPress custom functions plugin… ready to activate and use.

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 (MU-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 in the plugins table.

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:

Make your WordPress functions plugin a must-use plugin.

Let’s Put the Fun Back Into functions.php

Hopefully, now you know what you should and shouldn’t use the functions.php file for and that there are better alternatives to add code to WordPress, like using a WordPress custom functions plugin, creating your own plugin, or even using your child theme functions.php file.

Creating your own plugin to add custom functions to WordPress is always an option. If nothing else, it can make you look like a brainy WordPress developer by creating a plugin that will appear on your clients’ WordPress backend, or at least make you feel far better at coding than you actually are (at least, that’s how it makes me feel!)

For more useful information on what you should know about using functions.php,  check out our ultimate guide to the WordPress functions.php file. Or, if you really want to impress the WordPress community, why not learn PHP and start coding your own plugins and themes? It’s fun, functional, and free!