How to add code to functions.php to add new features to WordPress

Have you ever searched for how to do something in WordPress, such as add a new feature, and been told to copy code to functions.php? How do you do it? Is it safe? Follow this guide.

Learn how to add code to the functions.php file in WordPress to add new features. Use a plugin or do it manually with this guide.

What is functions.php?

Functions.php is one of the many files that make up a theme. It provides features and functions that the theme needs in order to perform actions and display content on web pages.

In some ways functions.php is like a plugin but it is specific to a theme. Every theme has its own version of this file and it contains functions unique to it.

You can view a theme’s functions.php file by going to Appearance > Editor. Select functions.php in the panel on the right-hand side.

View or edit functions.php in the WordPress editor

It can be useful to read through this and see what a theme is doing. A lot of it will not mean anything to you because it contains a lot of PHP code, but sometimes you can spot bits that make sense. For example, you might see a bit of code that resizes images you upload. This is useful for seeing what size images the theme prefers.

Manually add code to functions.php

The editor not only lets you view the code in the functions.php file, it lets you change it or add to it. Just click in the file and start typing! Actually, don’t do that because you might break your site. WordPress is not very forgiving of errors in the file, so you really need to know what you are doing to change anything or add new code. It can be done though.

The best way to add code to functions.php is to find code on the web that has been tried and tested by many people so that you can be sure it is 100% safe to use. Don’t try to type it in, copy it to the clipboard.

Go to Appearance > Editor and load functions.php. Scroll right down to the bottom of the file, position the cursor on a blank line just before the last line which reads ?> and paste in the code (Cmd+V or Ctrl+V).

Editing the functions.php file in WordPress

That boxed code above is a function I added to the end. It removes the Email and Website boxes from the comments form at the end of posts. Click the Update button at the bottom of the page to save the changes.

If the functions.php file does not end with ?> then just paste code at the end of the file.

Why you should not add code to functions.php

The file is part of a theme’s files and every theme has its own functions.php. Switch themes and the file with all your changes or additions is removed and replaced with the new theme’s functions.php.

If you update a theme then the same thing happens and the functions.php file in the update overwrites the old file and you lose any changes you made.

The way around this is to save any code you add to the file elsewhere. Keep a text file on the computer’s disk called MyFunctions.txt or something similar containing the code. If you change or update a theme, copy the code from MyFunctions.txt and paste it just before the last line in functions.php on your site.

Another solution and one which is preferable, is to use a plugin to manage the extra code you add. The code is stored in the plugin and not theme, so it does not matter if you change or update the theme.Plugins are unaffected by theme updates or changes.

Add code with Code Snippets plugin

Code Snippets is a free plugin that enables you to store the bits of code you would normally add to the functions.php file. Saving the code to the plugin means it will not be deleted when changing or updating themes. It has over 80,000 installs and an almost perfect score. Go and get it.

  1. Go to Plugins in the sidebar
  2. Click the Add New button
  3. Enter code snippets into the search box
  4. Click the Install Now button next to Code Snippets
  5. Click the Activate button

Select Snippets in the sidebar and click the Add New button. There is a box to enter the title and a box to type or paste the code.

The Code Snippets plugin in WordPress

A nice feature of the plugin is that it checks the code to see if there are any errors before it is saved. If it is wrong, the plugin displays an error. Fix it and then it will save.

To see the code you have added, select Code Snippets in the sidebar.

The Code Snippets plugin in WordPress

Code snippets can be activated and deactivated in a similar way to plugins, so it is easy to turn on or off features. You can export code to a file which you can then import on another WordPress website, which is great for people that manage several.

This is a very useful plugin for anyone that has added code to the WordPress functions.php file and is recommended.

Add code with My Custom Functions plugin

My Custom Functions performs the same task and it enables you to store code in the plugin and then use the plugin to add it to the functions.php file. It means it is protected when you update or change the theme.

  1. Go to Plugins in the sidebar
  2. Click the Add New button
  3. Enter my custom functions into the search box
  4. Click the Install Now button next to the plugin
  5. Click the Activate button

Accessing it is a bit strange and you need to go to Settings > PHP Inserter. This displays an editor into which you can type or paste code. Unlike the other plugin, all the code for all the functions appears here and there is a global On/Off switch.

My Custom Functions plugin for WordPress

This plugin has fewer features than the other one, but you may prefer the single editor where you can see everything on one screen. The plugin performs error checking to make sure you have not made a typing slip.