The What
Description
This construct fires the specified event, which means it loops through the event registry lookup table and calls each registered callback one-by-one. The callbacks are pre-registered for this event using add_action
.
Parameters
event_name
- The name of the event (filter hook) to fire.
...
- optional You are able to pass in one or more arguments to pass to each registered callback.
Return Values
It does not return anything, which is the main difference between an action and filter event.
Real-World ExampleBasic
Let’s take a look at a real-world example using the Genesis framework. Genesis uses do_action
a lot; therefore, it makes it a great candidate for showing you how it’s used.
Show It in ActionBasic
Let’s see the do_action
function in action. In this video, you will see how it works.
When was the last time you got out of your chair and stretched? Just saying…
Go Deep – OverviewPro
In this video, you will walk through do_action
in the WordPress Core source code and get a general overview of all of the tasks within this one function. In the sequent videos below, you will reverse engineer each of the sub-tasks in core.
A significant portion of this function is duplicated and works the same as apply_filters
.
Go Deep – ArgumentsPro
In these two (2) videos, you will walk through how the arguments are assembled and then sent to each registered callback. Remember that each one specifies how many arguments it wants when registering with add_action
.
There’s much to cover to really know how and why the arguments are processed and then sent to each callback.
Go Deep – Counting Number of Times the Action FiresPro
The first several lines of the function is responsible for counting the number of times each event is fired. This count is then used in did_action
.
Why are the apply_filters
shown below?
A significant portion of do_action
is duplicated and works the same as apply_filters
. Therefore, there is no sense in duplicating the teaching effort. Instead the remainder of the videos are for both do_action
and apply_filters
.
Go Deep – “all” EventPro
In this video you will explore and reverse engineer the “all” event as well as the function _wp_call_all_hook
.
This function is used in:
It works the same for all of these functions.
Go Deep – Check if in RegistryPro
In this video you will explore and reverse engineer WordPress Core
to see what happens if the event name does not exist in the registry.
Go Deep – What is $wp_current_filterPro
What is $wp_current_filter
? What is it’s purpose and intent? Let’s go deep into WordPress Core and discovery what it does and why.
You might have noticed from the previous video that both actions and filters are stored in $wp_current_filter
. Why? What is the purpose? Let’s discuss it to avoid confusion.
-
Code example
Hands off the keyboard. Web development starts by thinking first, then planning it out, and then coding it.
Go Deep – Sorting the RegistryPro
Before we can call each of the callbacks, first we need to sort the registry, i.e. $wp_filter
. Why? To make sure they are in the proper order by the priority level. Let’s dig into WordPress Core to look at who the registry is sorted.
Go Deep – Callback LoopPro
Now you will explore the hardest part of the Event Registry System, i.e. the callback loop. It is hard only because it uses quite a few PHP constructs that confuse people. Therefore, before you watch these two videos, stop and go watch these in the PHP Docx. Once you understand these constructs, then the callback loop will make so much more sense to you.
do { | |
foreach ( (array) current($wp_filter[$tag]) as $the_ ) { | |
if ( ! is_null( $the_['function'] ) ) { | |
call_user_func_array( $the_['function'], array_slice( $args, 0, (int) $the_['accepted_args'] ) ); | |
} | |
} | |
} while ( next($wp_filter[$tag]) !== false ); |
In this video, in addition to reverse engineering the loop, you will also learn about refactoring to improve performance (make it more performant). We do a couple of small tweaks to the code, which can have an impact on how fast the code runs. Let’s check it out.
-
Refactored version
PHP Constructs
These are the PHP constructs used in WordPress Core for do_action
: