You can create any type of website using WordPress as a content management system. The outlook of your site depends on the theme you choose to use. 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.
Even you get a perfect theme at the start, 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”. Remember, each them you have on your site will have a separate functions.php file.
Why Should You Edit functions.php?
Most of the time you have to edit the file for inserting functional code snippets. Let us take an example. Assume your theme registers 10 different image 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 three ways. The recommended way is to use FTP so that you can use your favorite code editor to modify the file. However, you can also edit through File Manager or through admin panel if that is convenient for you.
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.
File Manager from Hosting Account
- Login to your hosting account and locate File Manager app.
- You can find the file under “/wp-content/themes/” folder.
- Edit the file and save your 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.
Note: Some security plugins will disable theme editor in admin panel and you will not see the menu item in admin panel. In this case, either disable the option in your security plugin or use FTP or File Manager option.
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.
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.
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.
4 Comments
Leave your reply.