0
0
Wordpressframework~10 mins

Action hooks in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Action hooks
WordPress loads core
Action hook triggered
Check for functions hooked
No hooked functions
Continue normal flow
Continue normal flow
WordPress runs its core, triggers an action hook, then runs all functions attached to that hook before continuing.
Execution Sample
Wordpress
<?php
add_action('init', 'say_hello');
function say_hello() {
  echo 'Hello!';
}
?>
This code attaches a function to the 'init' action hook that prints 'Hello!' when WordPress initializes.
Execution Table
StepAction Hook TriggeredHooked Functions FoundFunction ExecutedOutput
1initsay_hellosay_hello()Hello!
2wp_footernonenone
3shutdownnonenone
💡 No more hooks triggered, WordPress finishes loading.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
hooknoneinitwp_footershutdown
functions_hookednone['say_hello'][][]
output"""Hello!""Hello!""Hello!"
Key Moments - 2 Insights
Why does 'say_hello' run only when the 'init' hook triggers?
Because in the execution_table at Step 1, the 'init' hook triggers and WordPress finds 'say_hello' hooked to it, so it runs. Other hooks have no functions hooked.
What happens if no functions are hooked to an action?
As shown in Steps 2 and 3 in the execution_table, WordPress triggers the hook but finds no functions, so it continues without output or changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after the 'init' hook runs?
A"Hello!"
B"Goodbye!"
C"" (empty)
D"Init hook triggered"
💡 Hint
Check Step 1 in the execution_table under Output column.
At which step does WordPress find no hooked functions?
AStep 1
BStep 2
CBoth Step 2 and Step 3
DStep 3
💡 Hint
Look at Hooked Functions Found column in the execution_table.
If we add a function hooked to 'wp_footer', what changes in the execution_table?
AStep 1 output changes
BStep 2 will show the function executed and output updated
CStep 3 will show the function executed
DNo changes at all
💡 Hint
Adding a hooked function affects the step where that hook triggers, see Step 2 in execution_table.
Concept Snapshot
Action hooks let you run your code at specific points in WordPress.
Use add_action('hook_name', 'your_function') to attach your function.
When WordPress triggers that hook, your function runs.
If no functions are hooked, WordPress continues normally.
Hooks help customize behavior without changing core files.
Full Transcript
Action hooks in WordPress are points where the system pauses and runs any functions you have attached. When WordPress loads, it triggers hooks like 'init' or 'wp_footer'. If you have used add_action to attach a function to a hook, WordPress runs that function at that moment. For example, attaching 'say_hello' to 'init' makes WordPress print 'Hello!' when it initializes. If no functions are attached to a hook, WordPress just continues without changes. This lets you add or change behavior safely and cleanly.