What is a hook in WordPress?
Hooks (called hooks in French) is a central concept for WordPress. It is present from the very beginning, they are already found in version 0.71 which is the first official version of WordPress.
These hooks allow developers to change the default WordPress behavior and add new features, all without having to touch the core files. They represent "spots" on which the developer can hang to execute specific code.
This approach has several advantages:
- Maintenance of WordPress: the core of WordPress is never changed, there is no risk of "losing" changes during updates,
- Code maintenance: the specific code is decoupled from the WordPress code. It is placed in plugins or theme.
Hooks are not limited to the heart of WordPress! Most of your extensions and themes offer theirs. So this is a big plus for the WordPress ecosystem in general.
They are divided into two large families, the shares and the filters. What's the difference?
Definition of hooks : actions vs filters
Before you go to practice and see how to take advantage of hooks, you have to understand the difference between the two main categories, actions and filters.
Actions
Actions are used to enable developers run code specific moments of WordPress heart code, extensions etc.
These are entry points so developers can "plug" their custom code.
The two associated functions are:
- add action : it allows developers to cling to an existing action,
- do action : it allows to trigger an action and execute the code that hung on it.
They do not allow to modify data, it is the role of filters.
Filters
Filters are used to allow developers to modify the data specific moments of WordPress heart code, extensions etc.
The two associated functions are:
- add filter : it allows developers to hang on to an existing filter to modify the associated data,
- apply filters : it allows to run the filter to let the code that hung on it change the data.
Unlike actions, filters take input data and must return it to output (or cause bugs in the project).
What are the hooks for in the heart of WordPress?
For example, WordPress provided the action "save post". This action is executed whenever a content is recorded. A developer might choose to use this action to send an email to the administrator to alert this change.
To take the example, WordPress provides the filter "wp insert post data" which allows to modify the data of a content before it is saved as a database. A developer could use this filter to modify the content.
How to use a hook in functions.php file?
To use a hook you go through the "add action" or "add filter" functions.
The signature of both functions is the same:
The name of the hook that we want to use is given first, and the second is a function that will be called when the hook is triggered (in English we call it a callback).
Rationale
Hooks can provide arguments for functions that are hung:
- in the case of actions, these arguments give context to the developer so that he can customize the behavior of his code,
- in the case of filters, the first argument corresponds to the value to be changed and the rest gives additional context, always to allow to customize the code behavior
The expected number of arguments in the hanging function can be specified if more than one is to be received.
Priority
Priority specifies an order in which the hook functions are executed. The lowest priorities are implemented first.
The default priority is 10.
The priority is to keep in mind when using filters, as each filter receives the result of the previous filter. If a filter changes the original value, the next filter will receive this modified value.
Practical case: add a message in the footer
To illustrate concretely the use of hooks, we will integrate the date of the day into the footer of your WordPress site. We will use the wp footer action that is embedded in the themes.
Good Practices for Hook Use
Where to place your WordPress hook code?
WordPress hooks are therefore the basis on which developers rely to make changes or add new features.
When working with WordPress hooks, choosing the location of your custom code is important to ensure the maintenance and robustness of your site. Two main options are available to you: plugins or your theme.
It is best to place your custom code in a plugin if its functionality should persist even if the user changes theme. This is the case for items such as custom content types, complex features, or any business logic that does not depend directly on visual appearance. A plugin ensures that these features remain active regardless of the chosen theme.
Conversely, if your code specifically concerns the appearance or behavior of your site related to a specific theme, it is wiser to integrate it directly into the theme (with the functions.php file in particular). This includes tasks such as declaration of menus, loading of theme-specific assemblies (style sheets, JavaScript scripts), or configuration of available Gutenberg blocks.
Where to find information about WordPress hooks?
We saw that hooks can have different numbers of arguments.
The official WordPress documentation (Developer Handbook) is a mine of information. It proposes a complete list of hooks, with details of their arguments and their use.
Nor should you hesitate to directly examine the source code of WordPress functions to see the hooks they offer (also apply in the case of plugin source code).
