WordPress is super flexible. You can add a new feature or even disable the ones you don’t like. The easiest way to add or disable a feature in WordPress is by using a plugin. But for a certain case, adding a custom function is more reasonable than installing a new plugin.
A simple instance.
Let’s say you want to disable the block editor (a.k.a. Gutenberg) as you deadly love the old editor. Instead of installing the Classic Editor plugin, which will add another plugin management task, you can add a custom function since it takes only a single line of code snippet to replace Gutenberg with the old editor.
There are at least three ways to add a custom function in WordPress. I will cover them all in this post and recommend which method is the best fit.
Shortcuts ⤵️
- What is a custom cunction?
- Method 1: By editing the functions.php file
- Method 2: Using a plugin
- Method 3: By create a custom plugin
- Summary
What is a Custom Function?
As I said above, WordPress gives you a total control over your website whereby you can add a new feature you need or disable a built-in feature that you don’t need.
One of the methods to add or disable a feature in WordPress is by adding a custom function.
As you know, the core of WordPress is written in PHP. And in the context of WordPress, a custom function is a PHP code snippet designed to add a certain feature or functionality to your website.
Adding a custom function is great in the following situations:
- You want to disable a certain WordPress feature (e.g., Gutenberg)
- You want to add a certain feature, yet don’t want to install a new plugin
3 Ways to Add Custom Functions in WordPress
1. Method 1: By Editing the functions.php File
Every WordPress theme has a file called functions.php. You can use this file to add a certain feature or functionality to your website via custom function.
Adding a custom function by editing this file is the easiest method since you can access the file via your WordPress dashboard. However, if the function doesn’t work properly, it can lead to an error on your website.
If you want to use this method, it’s highly recommended to create a backup of the original content of the file.
Another shortcoming of this method is that if you update your theme to the newest version, your custom function will lost as the update process usually overrides the existing content of the functions.php file.
Where Can You Access the functions.php File?
There are two ways to access the functions.php file, depending on the theme type use you.
Classic Theme
If you use a classic theme, you can go to Appearance -> Theme File Editor menu on your WordPress dashboard.
Select the functions.php file on the right panel to edit it.
You can place the custom function you want to add right after the last line of the content of the functions.php file.
Make sure to click the Update File button to apply the custom function you have just added.
Block Theme
If you use a block theme, you can go to Tools -> Theme File Editor on your WordPress dashboard.
The rest is the same as classic theme. You can select the functions.php file on the right panel and add the custom function you want to add to the editor area.
A little note if you use the Security Optimizer plugin by SiteGround. The plugin disables the ability to edit the theme and plugin files so you need to go to the settings page of the plugin first.
Some security plugins might also have the same default setting.
Method 2: Using a Plugin
If editing the functions.php file looks scary for, then here is an easier method you can opt to: using a plugin.
There are several plugins that you can use to add custom function. My recommendation is Code Snippets.
Not only you can add custom function, but you can also use Code Snippets to add custom CSS and custom JavaScript.
The advantage of adding a custom function using Code Snippets is that you can easily disable the custom function when you don’t need it and re-enable it when you need it.
Adding a Custom Function Using Code Snippets
First thing first, install the Code Snippets plugin and activate it right away once installed. Once done, you will see a new menu item called Snippets on your WordPress dashboard. Hover your cursor over this menu and select Add New.
Give your custom function a name and paste the snippet to the available editor. Click the Activate button to activate it right away.
To manage the code snippets you have added, you can go to Snippets -> All Snippets on your WordPress dashboard. From here, you can disable, enable, delete, and edit your snippets.
Apart from Code Snippets, there are several other plugins to add custom functions in WordPress.
Here is the list of the plugins that you can use to add custom functions:
- Code Snippets
- WPCode
- Woody Code Snippets
- Insert PHP Code Snippet
Method 3: By Creating a Custom Plugin
If none of the above methods suit you, then here is the last option you can choose to add a custom function to your website: by creating a custom plugin.
The plugin you are going to create is dedicated to hosting all custom functions on your website.
I myself use this method on this blog because using a plugin like Code Snippets is never an option to me.
When possible, I always avoid installing a new plugin to add a new feature and functionality to my blogs.
Creating a Custom Plugin to Host Custom Functions
First, create a new folder on your computer. You can name the folder anything you want.
Next, create a new text file using the default editor on your computer (Notepad on Windows and TextEdit for macOS).
Paste the following lines to the text file you are creating:
<?php
/*
Plugin Name: My plugin
Plugin URI: https://www.utilizewp.com
Description: Custom Plugin for adding custom code
Version: 1.0.0
Author: WPPagebuilders
Author URI: https://www.wppagebuilders.com/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
On the plugin name section (third line), you can replace it with a plugin name you prefer. This line will be used as the actual plugin name on the WordPress plugin manager.
Once done editing the above lines, save your text file with an extension of .php (e.g., myplugin.php). You can save the file to the folder you have created earlier.
Next, turn the folder into a ZIP file using any app you have.
Once your custom plugin is ready, go to Plugins -> Add New Plugins on your WordPress dashboard. Click the Upload Plugin button, followed by the Choose File button and select the ZIP file of your newly created custom plugin.
Click the Install Now button to install the plugin and activate it right away once installed.
Adding Custom Functions to Your Custom Plugin
Your custom plugin is now ready to host your custom functions.
To add a new custom function, go to Plugins -> Plugin File Editor on your WordPress dashboard (you can find Plugin File Editor under the Tools menu if you use a block theme).
Select your custom plugin from the dropdown menu and click the Select button to edit the file.
Add the snippet of your custom function to the editor area. End by clicking the Update File button to apply.
In the future, if you want to add new custom functions, you can place them right after the last line of the plugin file.
To make it easy for you to identify each custom function, you can add a comment right before the code snippet of the custom function as shown below.
Summary — Which Method is the Best?
Those are the methods that you can use to add custom functions to your WordPress website. The question is, which method should you use?
While I personally use the third method, you might have your own option. I prefer the third method because — as I said above — I always try to avoid installing a new plugin when possible.
Installing a new plugin means I need to deal with update notifications. Not to mention promotional notifications (when the plugin offers the pro version).
When I create a custom plugin myself, I don’t need to deal with any notifications.
What about the first method?
Unless you use a child theme, the first method requires you to be super thorough. When you update your theme and you forget to backup the content of the functions.php file, then you will lose your custom functions.