How to Add Code to Your WP Theme’s Functions.php File

Most WordPress themes have a file named functions.php that is located in the root theme folder (wp-content/themes/your-theme-name/). This file can be used for a lot of different purposes such as setting up theme widget locations, modifying default WordPress behaviors, use built-in WordPress hooks and filters, and much more.

A lot of tutorials and even some plugins provide instructions to add specific code to your theme’s functions.php file, but for new WordPress users, diving into PHP code can be a little bit nerve racking. 

We’re going to take you through the steps needed in order to open and modify your theme’s functions.php file so you can feel more comfortable editing your own PHP code.

Get Comfortable with FTP

WordPress does have a built-in theme file editor (Appearance > Editor) and you can use this to edit your functions.php code.

The problem arises when you edit the code and inadvertently create an error in your functions.php file. Best case scenario is you’ll just get an on-page error notice immediately after saving the file that you can easily correct back in the editor. Worst case scenario is you’ll have a fatal error or WSOD (White Screen Of Death) and you won’t be able to even access the editor (or anything else in the backend) to fix the error. As an added benefit, you just took down your entire site and it’s not accessible at all.

That’s where FTP comes in and why you should do any code edits outside of WordPress.  FTP simply stands for File Transfer Protocol and it’s just a way to connect to your server from your computer and transfer files back and forth. It runs independent of WordPress, so you can access your server files regardless if WordPress is functioning or not.

If you do not manage your own hosting account or do not have or know the account login credentials, you will need to have those in order to log into your site. You can log in using the main site credentials or have a new FTP account set up that can only access specific files and folders within the account.

FTP Clients for Windows and Mac

If you do not have a FTP client installed on your computer yet, here are some options for both Windows and Mac.

  • WinSCP – Free, full-featured FTP client for Windows only
  • FireFTP – Free Mozilla FireFox browser add-on for both Windows and Mac
  • FileZilla – Free FTP client for both Windows and Mac
  • Transmit – Popular Mac FTP client but not free
  • CyberDuck – Open source FTP client for Mac and Windows

We won’t get into the features or details of each client, but you can read about each one and pick the one you think will fit you best. For ease-of-use, we probably recommend either FileZilla or FireFTP (if you have Firefox browser installed).

Hosting Account File Manager as an Alternative to FTP

If you are comfortable working within your hosting account’s file manager, you can also edit the functions.php file there as well. The majority of web hosts use a cPanel configuration and within the cPanel you have access to a File Manager utility. cPanel alternatives like Plesk also normally have some type of file manager as well.

The File Manager allows you to view files, edit code, upload and unzip files, create new directories, and more. So if you prefer to edit code directly within the File Manager, it’s fine to do so as an alternative to using FTP.

Use a Child Theme for Functions.php Changes

Any time you update a WordPress theme, all files are replaced with new versions. This causes a problem when making changes to the functions.php file because if you update your theme, you will lose any custom code that you have added.

The solution to this is to use a child theme when making theme customizations. If you are not familiar with child themes, the WordPress Codex has a lot of information on creating one.

All you will need to add custom functions.php code via a child theme is a style.css file that identifies the parent theme, and a functions.php file that contains the code required to load or ‘enqueue’ the parent theme’s stylesheet.  

Creating a child theme requires no special software as a simple text editor will work fine. You start by creating a new folder on your computer, let’s call it ‘my-child-theme‘.

Then create two text files inside the new folder, one will be named style.css and the other will be named functions.php

The Codex outlines the code required for both the child theme style.css and the code for the functions.php file.  The example given in the Codex creates a child theme for the Twenty Fifteen theme and contains quite a bit of extra information that is not required for your child theme. 

Example style.css code for Twenty Fifteen child theme (simplified from Codex):

In order for this code to work with your theme, you will need to change the Theme Name to something descriptive for your theme (i.e. My Child Theme), and the Template value to match the name of your parent theme folder.

You can find the name of the parent theme folder by viewing the source code from your site (right click in your browser and choose ‘View Source’ or ‘View page source’) and looking at the current style.css link URL. The link will look something like this:

<link rel=’stylesheetid=’some-theme-csshref=’http://www.yourdomain.com/wp-content/themes/parent-theme-folder/style.css?ver=1.0.0type=’text/cssmedia=’all‘ />

The ‘parent-theme-folder’ value is what you are looking for and it will be unique to your current theme. Add that value exactly as it appears in the link in the Template value in your child theme’s style.css file. Save the file and you’re done with it.

The next step is to add code to the child theme’s functions.php file that will load the parent theme’s stylesheet after the child theme is activated.

The required functions.php code looks like this:

You can copy/paste this code into the blank functions.php file you created in your text editor. Save it and you’re done with it.

Once you have added the customized style.css code and the code above into the functions.php file, you can zip up the ‘my-child-theme’ folder (be sure you zip the folder and not just the two files) and upload it into WordPress using Appearance > Themes > Add New.

The child theme doesn’t have a screen shot image included, so there will be no image for the child theme in the Themes page. Once activated, you should be able to refresh your site in your browser and it should look no different from when the parent theme was activated.

Now you are ready to add your custom code to the functions.php file and there is no need to worry about theme updates wiping out your code changes.

Add New Code to Functions.php

The hard work is done now that you have created a child theme and installed/connected an FTP client.

Editing the functions.php file can be done in either a basic text editor or any HTML editor. 

When you open the functions.php file for editing, any code that you add will go inside the closing ?> tag. So for example, we’ll add PHP code that changes the default WordPress excerpt length from 55 words to 100 words.

Here’s the code to change the default excerpt length:

And after adding it to the functions.php file, the file looks like this:

The /* Change Excerpt Length */ line is a comment and doesn’t do anything other than help you navigate your code. It’s good practice to comment each new addition to the functions.php file so you can recall exactly what the code was added for. You can also make a comment by prefacing the comment with two slashes like: // Change Excerpt Length.

Anytime you edit a PHP file, always make sure you remove any extra lines at the end of the file after the closing ?> tag. Extra whitespace can cause errors that are very difficult for beginners to troubleshoot.

PHP code uses a lot of parentheses () and curly brackets {} and for every one that is opened you have to close it. If you’re worried about having a syntax error in your code, there are a number of online PHP code checkers available that can assist you in writing your code. Some examples are:

Useful Code for Functions.php

Here are some code examples that you can use in your functions.php file.

Remove WordPress Version Number

By default, WordPress tags the end of every script and CSS file link with the version number if no other version is specified. This code removes the WordPress version and helps prevent hackers from exploiting older versions of WordPress.

Custom Excerpt Link Text

This replaces the default (…) elipses at the end of an excerpt with your choice of text.

Disable WordPress Login Hints

When a failed login occurs, WordPress tells you if the user name or password is incorrect. This can alert hackers to a valid user name and allow them to perform a brute force attack on your password. The following code shows a custom message on a failed login attempt.

Enable Shortcodes in Widgets

Widgets handle shortcodes as if they were regular text. But adding the code below to your functions.php file allows text widgets to run shortcodes as they should.

Reduce Number of Saved Post Revisions

Every time you save a post or page, WordPress saves a revision. Over time these can pile up and slow down your database. To set a fixed number of saved revisions, add this code to your functions.php file. Change the 3 value to however many revisions you wish to save.

Exclude All Pages from Search

Many times you have pages that you don’t want to show up in search. To exclude pages from showing in a search add this code.

Exclude Specific Pages from Search

Sometimes you may only want to exclude a few pages from the WordPress search function.  Here’s the code that will do that. Just update the page ID’s (18,29,75) with your own ID’s of the pages you want to not show up in a search.

Tutorial Wrap Up

Adding code to your theme’s functions.php file is not hard, but doing it the right way using a child theme takes a little bit of time up front, but will save you a ton of headaches down the road.

The functions.php file is a powerful one that can make or break your site, so be sure to check your code for syntax errors before updating the file.  And be prepared to revert back to a working version by copying the file prior to making changes. This can be done as simply as copying the remote file to your computer or even just doing a copy/paste into your text editor until you get your edits completed and verify they are working properly.

We hope you enjoyed the tutorial and happy coding!

Enjoy the Read? Share some Love!