• Skip to content
  • Skip to footer
A Bright Clear Web

A Bright Clear Web

A Bright Clear Web - Simple, effective, accessible websites

  • Home
  • About
  • Websites
  • Writing
  • Learn
  • Blog
  • Contact
You are here: Home / WordPress / How To Add Functions To Your WordPress Site Safely

How To Add Functions To Your WordPress Site Safely

Posted: October 14, 2016 Updated: May 3, 2018 by Claire Brotherton
3 Comments

My Custom Functions fatal error

What is a theme’s functions.php file?

The functions.php file is a key part of any WordPress theme. It’s where the code runs that can do things like:

  • set up the theme
  • add widget areas
  • register theme fonts
  • add support for the WordPress Customizer
  • add menu support

It’s an essential file, and susceptible to breakage.

Sometimes we might want to add our own functions to a theme to extend its functionality, or to change the output of a plugin.

File changes should never be made in a parent theme, as the changes will be overwritten the next time the theme is updated.

Usually, the recommendation is to make a child theme and to add the function to the child theme’s functions.php file.

You could do this by:

  • editing the child theme’s functions.php file in Appearance > Editor and saving it.
  • downloading the file via FTP, adding the new function through a text editor and re-uploading it (I’d recommend this approach. Keep a copy of the original file).

However, if you make a mistake with typing the code, or copy and paste from another source and there’s an error, this will happen.

The page isn't working - 500 error
500 error due to error in functions.php file

You won’t be able to access the site or the wp-admin!

Ouch!

Fixing a 500 error caused by a faulty functions.php

Normally, if you edit a theme file and make an error, you can sort it by going to Appearance > Editor in the menu, select the file and edit it.

This won’t work for functions.php, though, as any error within it completely locks you out of your site.

You’ll need to access your site via a FTP client, or your hosting control panel. I prefer FTP.

Remote site files in Transmit FTP client

Log in, find the theme folder and upload a backup copy of your functions file there (you did make a backup?).

Or else download the functions.php file, delete the function you added  and re-upload it.

Avoiding editing functions.php on a live site

As you can see, working on a live site’s functions.php is risky.

There are a couple of  better approaches if you want to add functions to your WordPress site safely:

  1. Using a plugin to add functions (good).
  2. Adding functions on a staging site (better).

Adding functions safely with My Custom Functions

There are a number of plugins that do this job.

I like this one, My Custom Functions, by Arthur Gareginyan.

Once you install it, go to Appearance > Custom Functions, enter your function code and hit Save Changes.

If there is any fault in the code, it will not be applied and you’ll get a warning.

My Custom Functions fatal error
A fatal error was detected, so the code is not applied

Your site will remain intact – phew!

The one thing the plugin doesn’t tell you is where your code has gone wrong.

Check over your code and see if there’s a simple fix.

Common issues are leaving out a semicolon ; or a curly brace {}.

In this case, I left off a { in line 11.

When to use My Custom Functions

  • You simply want to add a single function or two to your site and don’t want to create a child theme.
  • You are already using a child theme e.g. a Genesis child theme and don’t want to risk adding a function that might break your site.

Working on a staging site

If you are doing more extensive customisation to your theme than adding one or two functions, a better approach is to create a staging site.

In this case, you won’t use the custom functions plugin. You’ll be working directly on a copy of your site. Because it is a copy, you don’t risk damaging your live site.

Your options here are:

  1. Create your own
  2. Use a service such as WP Stagecoach
  3. Use a managed WordPress host with staging as part of its service offering e.g. Kinsta or Flywheel.

You can find out how to make your own here: How to Create Staging Environment for a WordPress Site.

When you’re working on your staging site, you’ll want to edit the wp-config.php file to turn debug mode on.

Find the line that says

define('WP_DEBUG', false);

and change it to

define('WP_DEBUG', true);

Then if you make a mistake and get a 500 error page, you can see where the error lies.

Example of a WordPress parse error
Error reporting turned on in wp-config

Remember to turn off error reporting when you’re done working on the theme, especially if you plan to push your site directly from staging to live.

I hope this post helps you understand how to add functions to your WordPress site safely. If you found it useful, please share!

Related

Category: WordPress Tags: functions

About Claire Brotherton

Freelance web designer and front end developer based in Edinburgh, Scotland. I love WordPress, code, learning and blogging.

Reader Interactions

Comments

  1. Karim says

    August 19, 2018 at 10:01 pm

    Hey Clear WEb also you review the Custom functions option?

    https://wordpress.org/plugins/custom-functions-littlebizzy/

    Reply
    • Claire Brotherton says

      August 19, 2018 at 10:19 pm

      Hi Karim,

      Thanks for reading!

      Haven’t tried that one! Have you used it?

      Claire

      Reply

Trackbacks

  1. How To Add Functions To Your WordPress Site Safely | World of WordPress says:
    February 13, 2017 at 4:06 pm

    […] What is a theme’s functions.php file? The functions.php file is a key part of any WordPress theme. It’s where the code runs that can do things like: set up the theme add widget areas register theme fonts add support for the WordPress Customizer add menu support It’s an essential file, and susceptible to breakage. Sometimes we… Continue Reading How To Add Functions To Your WordPress Site Safely […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email. Read my privacy policy.

potted cactus and laptop with code

Footer

Categories

  • Accessibility
  • Blogging & Content Marketing
  • Business
  • Personal
  • Software & Tech
  • Websites
  • WordPress

Top Posts

  • How To Create WordPress Excerpts And Include Links In Them
  • How To Solve WordPress Image Alignment And Text-Wrap Problems
  • Improve Your Divi Website's Reach With Divi Accessibility
  • How To Set Up And Customize Twenty Nineteen Theme
  • Registering a Domain Name with GoDaddy - Step by Step

Let’s Be Social

  • E-mail
  • Facebook
  • LinkedIn
  • Twitter
Copyright © 2014 -2025 A Bright Clear Web
  • Privacy and cookies policy
  • Site map