How to Edit functions.php File and Add New Functions in WordPress?

You can create any type of site in WordPress by choosing different types of themes. A theme gives the required look and feel to your WordPress site and makes it relevant to the niche. But the problem is, it is impossible to choose a perfect theme that can satisfy all your needs.

At any case when your site grows, you will need many additional functions to control the site’s behavior. In this article let us explore more on what is functions.php file and various ways to edit functions.php file in WordPress.

What is functions.php File in WordPress?

A theme has many functions, like how many thumbnails to be generated for each image, how many sidebars the layout should have, etc. Each required function is to be registered inside a file called functions.php. This is a controller file of your theme to produce the needed outputs.

This file is located under “/wp-content/themes/your-theme/functions.php”.

Why Should You Edit functions.php?

Let us take an example. Assume your theme registers 10 different images sizes for showing on different places like below:

  • 4 image sizes for inside content
  • 4 image sizes for header slider
  • 2 image sizes for sidebar widget

But you decided not to use the header slider offered in the theme. In this case, every image will have 4 unused thumbnails in your images folder.

The best way is to deregister those unused image sizes in order to save storage space. In this case you should search the relevant image sizes in your theme’s functions.php file and remove or comment them.

How to Edit functions.php File in WordPress?

You can edit the file in two ways. One is using FTP and other is through admin panel.

Using FTP
  • Launch your favorite FTP client and connect to the hosting server remotely.
  • Navigate to “/wp-content/themes/” folder.
  • Open your active theme and locate functions.php file.
  • Either you can download, edit and then upload or directly open in text editor to modify the content.
Using Admin Panel
  • Login to your WordPress admin panel and navigate to “Appearance > Editor”.
  • Locate functions.php file and modify the content.
  • Save your changes.
Edit functions.php in WordPress Admin Panel
Edit functions.php in WordPress Admin Panel

When You Should Not Edit functions.php File?

The above image register example is a perfect case for editing functions.php file. But there are many situations you just want to add additional functions without modifying existing functions.

For example, you wanted to have a custom post type for testimonials. In this case, testimonial posts should exist on your site even you change the theme. So creating a custom post type is beyond the scope of your theme’s scope.

Here it is not recommended to modify the theme’s functions.php file. You should have two options to register a custom testimonial post type for your site.

Using Functionality Plugin

The functionality plugin is a custom plugin you can create on your own in order to add new functions to your site. You don’t need to edit the theme’s functions.php file.

Let us take an example that you want add a function to stop WordPress heartbeat API. Here is how you can create functionality plugin. First create a php file and name it as stopheartbeat.php.

Add the following details in the php file; you can replace the content as per your need.

<?php
/*
Plugin Name: Functionality Plugin for Stop Heartbeat API
Plugin URI: https://www.webnots.com/
Description: Stop heartbeat API to disable admin AJAX calls
Author: WebNots
Version: 1.0
Author URI: https://www.webnots.com/
License: MIT
*/

Below the plugin details section add the function code for deactivating heartbeat API.

add_action( 'init', 'stop_heartbeat', 1 );
function stop_heartbeat() {
wp_deregister_script('heartbeat');
}

The entire stopheartbeat.php file should look like below:

<?php
/*
Plugin Name: Functionality Plugin for Stop Heartbeat API
Plugin URI: https://www.webnots.com/
Description: Stop heartbeat API to disable admin AJAX calls
Author: WebNots
Version: 1.0
Author URI: https://www.webnots.com/
License: MIT
*/
add_action( 'init', 'stop_heartbeat', 1 );
function stop_heartbeat() {
wp_deregister_script('heartbeat');
}

Launch your FTP client and navigate to “/wp-content/plugins” folder. Create a new directory and name it as “stopheartbeat”. Upload the “stopheartbeat.php” file inside the folder.

If you don’t want to use FTP then compress the “stopheartbeat” folder to a zip archive. Upload the zip file as a new plugin from admin panel.

Installing Functionality Plugin
Installing Functionality Plugin

That’s it you are done. Your functionality plugin is “stopheartbeat” which will work perfectly to stop heartbeat API. You can view the new plugin is added under “Appearance > Plugin” in your admin panel.

Active Functionality Plugin
Active Functionality Plugin

You can give any generic name to your plugin / PHP file and add any functions like registering a new custom post type.

Using Child Theme

Child themes are the recommended way when you have to change multiple php files and stylesheet of your theme. For example, you can modify header.php, style.css and functions.php (adding new functions) within a child theme. You don’t need to edit parent theme’s files at all.

Child theme works similar to functionality plugin. Any function added to the functions.php file inside a child theme will be loaded in addition to parent theme’s functions.php file. You can refer our separate article on how to create a child theme.

Remember Before Editing functions.php File

  • It is not recommend editing core theme files, as the changes will be wiped off whenever you update the theme.
  • Purpose of functions.php file is to control only the specific theme. So only add functions in the file if they are relevant to the theme level.
  • For the functions not relevant to theme like adding shortcode, use functionality plugin or child theme.
  • Generally functions.php file does not have the closing PHP tag to enable adding the content at the end of the file.
  • Remember wrongly making a one small dot inside functions.php file will take your entire site down. Most probably you will see the PHP error on the browser mentioning the line number that caused the error in functions.php file or you will see a white screen of death.
  • There are also dedicated plugins like my custom functions for adding custom functions on your site. You can also use the plugin instead of creating your own functionality plugin.

Editorial Staff at WebNots are team members who love to build websites and share the learning with webmasters community. Follow us on Facebook and Twitter.

2 thoughts on “How to Edit functions.php File and Add New Functions in WordPress?”

Leave your response.

  1. I feel that making alterations/additions is fine…. up until the point a newer version of the theme is installed. At this point all changes are lost.
    There are several methods of keeping your mods “safe” from upgrade, however most seem complicated and long winded.
    The very quick solution I use is –
    1. In the same folder as the functions.php file create a new file called “customcode.php”*
    2. On the very last line of functions.php add the line – include “customcode.php”*
    *Obviously you can create your customcode in another location, but this must be reflected in the include statement
    3. Include all of your additions/alterations in customcode.php
    When a new version of the theme becomes available there is only ONE change to be made to the new theme’s files… in functions.php (re) add the include line to your customcode
    Much easier than some of the other options…
    Cheers
    Geoff

    Reply

Leave a Comment