The What
Syntax
bool add_action ( string $event_name, callable $callback_name, integer $priority = 10, integer $number_of_args = 1 );
Description
Registers your callback to the specified event (action hook). When the event fires, the callback is called per the priority specified, and the number of arguments are passed to it. This construct is part of the Plugin API. It allows WordPress, plugins, and themes to be customized, changed, and extended, providing a more rich user experience.
Parameters
event_name
- The name of the event (filter hook) you want to register your callback to, e.g.
init
. Here is a list of all the available action events in WordPress. callback_name
- The fully qualified name of your callback. This is the callback you want to register to this event when it fires.
priority
- optional Specifies the calling order with the lowest number being called first. It allows you to determine when you want your callback to fire. It defaults to 10.
number_of_args
- optional This option indicates how many arguments you want passed to your callback. It defaults to one (1).
Return Values
Returns TRUE
if your callback is successfully registered; else you’ll get a FALSE
back.
Show It in ActionBasic
Let’s see the add_action
function in action. In this video, you will see how it works.
NamespacingBasic
Let’s talk about namespacing in relationship to registering a callback with add_action
.
Event Lookup TableBasic
Your callback is registered into the Event Lookup Table when you use add_action
You get WET when you swim. Stay DRY when you code.
Calling Too LateBasic
You have to pre-register your callback before the event occurs (fires). In this video, we’ll talk about this and show you what happens.
Go DeepPro
In this video, you will go deeper into the instruction by looking at WordPress Core.
Registering Method or ClosurePro
In this video, you see how to register the following callbacks:
- An object’s method
- Static method
- Closure
WhenPro
PHP Constructs
These are the PHP constructs used in WordPress Core for add_action
: