The functions.php file is part of every WordPress theme. Which holds the various functions your WordPress theme register to use. In case you are facing some critical error in WordPress cause of a broken function or needs to add a new function in functions.php file. If you are newbie you may think How to edit functions.php in WordPress? Well its not that difficult or require high technical skills. Our guide will make it really easy for you to edit your functions.php file of WordPress theme.
The WordPress theme’s functions.php file is written completely in PHP language. So before you edit it you need to know what is a PHP function and how to not break any existing function or write a broken PHP function. Otherwise you can put your site into a WordPress error There has been a critical error on this website. Which is a error WordPress shows when there is a FATAL PHP error.
What is functions.php File in WordPress theme?
There are various things which a theme provides you. For example theme have some interactions through javaScript and those JS files are enqueued through functions.php. Similarly if theme needs to register new stylesheets that enqueue WordPress stylesheets via functions.php. Some other functions like registering custom post types, metaboxes, comments functions, pagination, breadcrumbs, theme options, customizer options. These type of features are included through functions.php in a WordPress theme.
Some developers directly write the code in functions.php if the code is not very lengthy. But some will divide different functionalities in different files and include them by require_once or include_once in functions.php. So basically the functions.php is the engine of your WordPress theme’s functionality. Above is a preview of functions.php file.
When do you need to edit functions.php file in WordPress?
In case you are trying to add new thumbnail size into your WordPress theme. Or you are going to register a new custom post type. Maybe you are trying to add a metabox or extending your theme options or customizer options. Or you are getting an error in your website because of theme’s functions.php file. Or you want to enqueue a new font family or stylesheet or script file. In case you are trying to change the label of your archive title in breadcrumbs?
As everything listed above is handled through functions.php which tells WordPress how to handle this data in this WordPress theme. These are some reasons when you are in need to edit functions.php file of your WordPress theme.
Let’s explore now how?
How to edit functions.php file in WordPress Theme?
Well till here you are well aware what is a functions.php file and why do you need to edit it. So now its time to learn how you can edit functions.php file of your WordPress theme.
As you may already know there are 3 main ways to edit functions.php. From WordPress admin, via FTP and via cPanel file Manager. Whatever is your preferred way you can use it as everyone like to work as per their choice.
If you ask me how do i edit functions.php well! If i am creating a new theme then obviously FTP is the best way cause the code i am adding is lengthy and can produce problems. But if i am adding a small line or feature or function then i prefer WordPress’s Theme File Editor.
Here is a video which can guide you through editing functions.php file easily!
So now are you well aware about the ways to edit the functions.php? Let’s cover up them one by one hopefully you find it easy to understand.
1. Through WordPress Admin
When you have basic knowledge of PHP and you are sure you wouldn’t break anything. You can easily edit funcitons.php from your WordPress admin panel. The simple steps are listed below to follow.
- Sign in to your WordPress admin panel and access /wp-admin/
- Now go to Appearance >> Theme File Editor
- Make sure the right theme is selected
- Click on Theme Functions labeled functions.php file
If you receive an error when you click update file something like “Your PHP code changes were rolled back due to an error on line 28 of file wp-content/themes//functions.php. Please fix and try saving again.” That means your code have some syntax error so fix it.
Remember WordPress can only give you warning about syntax error. If your code calls a function which is not defined you are going to face a FATAL error which would produce Critical error in WordPress.
2. Edit functions.php in WordPress via cPanel File Manager
Before you edit the functions.php file via cPanel File Manager there are few things you need to know. As you know WordPress wp-content/themes folder have several themes and only one of them is an active theme on your site. So there are two things you need to confirm before going forward with this.
- What is the name of active WordPress theme?
- What is the root of your WordPress installation?
As you know you can have many WordPress installations so make sure you know which installation’s WordPress themes folder you need to access. Now i assume you know what is the name of your active WordPress theme and what is your WordPress installation root. Let’s now edit functions.php via cPanel
- Sign in your Web Hosting Panel either its cPanel or any other
- Access File manager and move to your WordPress installation folder
- Go to wp-content/themes and open your active theme’s folder
- Right click on functions.php file and edit it.
- Once you have made changes you wanted save it
3. How to edit functions.php in WordPress Via FTP
So let’s now try to edit functions.php file via FTP its best way when you are writing a new code or functions. Its easy to quickly update single file and edit it locally by downloading through FTP.
Things you would need to edit functions.php through FTP are.
- FTP Client like FileZilla
- Know the active theme in your WordPress
- Know the root of your WordPress installation
- A code editor like Visual Code Editor
Here i assume that you are connected via FTP client with your server. You already know the active theme on your site cause WordPress themes directory can have many themes. Also you know the root of your WordPress installation. Let’s now edit the functions.php file via FTP now.
- Login to your FTP by providing Host, Username and Password
- Go to your WordPress Installation Directory
- Move to wp-content/themes directory
- Now identify your theme’s directory by its name and open it
- Download the functions.php file
- Make changes using your favorite IDE and save to re upload!
That’s it how easy it was if you followed the steps carefully! Its always recommended to keep the backup of important files in case anything goes wrong its easy to revert back.
Things you should understand before you edit functions.php File in WordPress
As you know functions.php is purely a PHP file which calls and defines new PHP functions. You should always make sure before calling a function that function does exist. And before you create a new function always check if that function doesn’t exist already in any plugin or WordPress itself. Else the conflict can bring the critical error.
How to check if a PHP function doesn’t exist already?
You can easily wrap your function which you are defining inside PHP’s default function if function_exists like below.
if ( ! function_exists("my_function_name") ) :
function my_function_name() {
//This function will only be defined if there is not any same name function available already
}
endif;
Use Child theme to add new functions in WordPress theme’s functions.php
When you are using a Premium WordPress theme or a theme from WordPress.org. Then there are chances a future update related to security, feature or improvement may release. And if you believe that you would need to update your theme that time. Then i highly recommend you to not edit your theme’s functions.php file directly otherwise you will lose your updates, edits or additions.
In such cases always use a Child theme in WordPress. Cause if the parent theme even updates your changes or updates in child theme wouldn’t go anywhere.
Summary
In this tutorial we tried to explain different ways how you can edit or update your theme’s functions.php file in WordPress. While there are 3 main ways from WordPress admin panel theme file editor. Also via File Manager in your hosting panel or via FTP client. But its completely upto you whichever method you use cause everyone have their own choice and taste. But you should always be careful editing functions.php to avoid any mess. If you need support from a developer Hire a WordPress developer today!
2 Comment(s)