Understanding the essence of two key functions - add_action and do_action - is fundamental for developing projects on WordPress, because they form the basis of the architecture of writing and outputting code on this platform.
WordPress has a powerful hook system that allows developers to add and change functionality without editing the main code. It is these two key functions in this system that solve these problems.
What is add_action?
add_action
is a function that links your custom function to a specific WordPress hook. It is used when you want your feature to be triggered at a certain point in the WordPress lifecycle, for example, when loading a page or saving a post.
Example of using add_action:
function my_custom_message() {
echo 'This is my custom message!';
}
add_action( 'wp_footer', 'my_custom_message' );
In this example, we create a my_custom_message
function that outputs a message. Then we link this function to the wp_footer
hook so that it runs at the bottom of the page.
What is do_action?
do_action
is a function that calls all functions registered to a specific hook using add_action
. You use this function in the place where you want to initiate the execution of all hook-related functions.
Example of using do_action:
function another_function() {
echo 'This is a different function!';
}
// Adding it to our hook.
add_action( 'my_custom_hook', 'another_function' );
// Calling our own hook.
do_action( 'my_custom_hook' );
In this example, we add the another_function
function to our own hook my_custom_hook
. Then, when we call do_action('my_custom_hook' )
, WordPress will perform all functions related to this hook.
Full example code
Now let's combine everything in one example, which can be placed in a file.functions.php
of your WordPress theme:
// 1. Defining the function that will be called via wp_footer.
function my_custom_message() {
echo 'This is my custom message!';
}
// 2. Add this function to the wp_footer hook.
add_action( 'wp_footer', 'my_custom_message' );
// 3. Define another function.
function another_function() {
echo 'This is a different function!';
}
// 4. Add this function to our own my_custom_hook hook.
add_action( 'my_custom_hook', 'another_function' );
// 5. We call our own hook to display the message.
do_action( 'my_custom_hook' );
How does it work?
When you load a page, WordPress performs the following steps:
- When
wp_footer
is reached, it will execute themy_custom_message
function, outputting the message:'This is my custom message!'
. - Anywhere in the code where you call
do_action('my_custom_hook' )
, WordPress will execute all the functions added to this hook, includinganother_function
, outputting:'This is a different function!'
.
The fundamental difference between these two entities is do_action and hooks
do_action is a function that calls all functions registered for a specific hook. It is not a hook in itself; rather, it is used to initiate the execution of functions linked to the hook.
The hook in question is the name that you pass as a string (for example, 'wp_footer' or 'my_custom_hook'). This is the hook itself, and this is the key name when you use the add_action and do_action functions.
Comparison:
do_action('my_custom_hook'): It's a challenge that means: "Perform all the functions that were added to the hook named 'my_custom_hook'."
add_action('my_custom_hook', 'my_function'): This is adding the function 'my_function' to a hook named 'my_custom_hook'. Here 'my_custom_hook' is the hook itself.
In other words:
Creating a hook:When you write add_action('my_custom_hook', 'my_function'), you create a link between the hook'my_custom_hook' and the function'my_function'.
Calling the Hook:When you write do_action('my_custom_hook'), you are calling all the functions that were associated with this hook. That is, if a function has been added to the 'my_custom_hook' hook somewhere, it will be executed when you call do_action.
That is:
do_action is a function for calling (holding) all functions associated with a specific hook.
Hook is the name of an extensibility point in WordPress to which functions can be linked.
Using add_action
and do_action
allows developers to create flexible and extensible solutions in WordPress themes and plugins. These are powerful tools that allow you to add functionality and change behavior without having to edit the source code.