wordpress hooks: user guide

Before moving on to understand the hooks in wordpress, let’s try to dwell for a moment on the need that you will certainly have had to change something in wordpress or customize part of the active theme. It is probably not to be excluded that you will certainly have done countless searches to find an exhaustive guide on the matter, but that you have not yet managed to find something that fully illustrates this fantastic mechanism that integrates wordpress since its very first versions.

1. The hooks

First of all, it is essential to directly modify the theme scripts and above all the functions.php script, it is necessary to point out that by removing the theme you will also lose all the changes you have made to the functions.php file. So let’s go ahead and see what hooks are and why they can help us customize wordpress.

Now you will discover that there is a surefire system to change the behavior of the CMS and this system makes extensive use of the Hooks. We will try to understand the logic that pushed wordpress developers to introduce this tool.

WordPress hooks are nothing more than hooks, to which the developer can attach himself to modify the nature and functionality of wordpress. They are also indispensable when you want to extend the possibilities for customizing a theme.

WordPress has a series of pre-established hooks, but depending on the theme they can be used only partially or almost. Therefore it is necessary to install a free plugin that will show us the names of the hooks and the positions within the active theme.

2. The plugin Simply show hooks

So, first you need to install a plug-in, the plug-in in question is Simply show hooks.

hook in wordpress

After activating this plug-in in the menu at the top of wordpress the Simply show hooks item will appear, at this point we activate the Show Action Hooks option, and we go to the “Appearance” menu item and click on “customize”, the home page of the site will open with all the green hooks highlighted in the points where we will be able to insert our code.

At this point just take note of the name of the desired hook and by clicking on “Appearance” -> “theme editor” we will find the functions.php file, click on it and wordpress will open it for editing.

hook in wordpress

at this point through the add_action system function to which we will pass two parameters, the first is the name of the hook, the second is the function that will be invoked and the result of which will be inserted at that precise point of the theme.

3. The first function hook

Let’s take a practical example, insert this code at the end of the functions.php file:

function Hello_world() {
   echo 'Hello World';
}

add_action('nameHook', 'Hello_world'); 

We will notice that in the precise point of the theme where our hook is displayed the string Hello World will appear.

What is important to know is that it is possible to hook multiple functions to each hook, and that with the remove_action function (‘nomehook’, ‘nomefunction’); we can unhook the execution of the function passed as a parameter to the hook. It will always be possible to stop the display of the hooks by pressing the link at the top right “stop showing hooks”:

hook in wordpress

You will be interested to know that you can also create personalized hooks for you, in any case watch this video and you will realize how simple it can be to manage them.

Creating dynamic WordPress Hooks

Antonio Lamorgese

Network administrator and developer. After years of experience in the industry, I designed a MYSQL data management system in PHP without writing a single line of code. Find out more....