0
0
Wordpressframework~10 mins

add_action and add_filter in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - add_action and add_filter
WordPress loads core
Hooks registered with add_action/add_filter
WordPress runs event or filter point
Check for hooked functions
No
Skip
If filter, modify data and return
Continue WordPress execution
WordPress loads and registers hooks with add_action or add_filter. When an event or filter point runs, WordPress checks for hooked functions and runs them in order. Filters modify data and return it.
Execution Sample
Wordpress
<?php
add_action('init', 'say_hello');
function say_hello() {
  echo 'Hello!';
}
?>
This code hooks the say_hello function to the 'init' action, so 'Hello!' prints when WordPress initializes.
Execution Table
StepHook PointHooked FunctionAction TakenOutput/Result
1WordPress loadsadd_action('init', 'say_hello')Registers say_hello to 'init'No output yet
2WordPress reaches 'init' actionsay_helloRuns say_hello functionPrints 'Hello!'
3No more hooked functionsN/AContinue normal WordPress flowNormal page loads
💡 All hooked functions for 'init' run; WordPress continues normal execution
Variable Tracker
VariableStartAfter Step 1After Step 2Final
hook_registry['init']empty['say_hello']['say_hello']['say_hello']
output_bufferemptyempty'Hello!''Hello!'
Key Moments - 2 Insights
Why doesn't 'Hello!' print immediately after add_action is called?
Because add_action only registers the function to run later at the 'init' hook point. The function runs only when WordPress reaches that hook (see execution_table step 2).
What is the difference between add_action and add_filter?
add_action runs functions at specific points without changing data, while add_filter runs functions that receive data, modify it, and return it. Filters change data flow; actions do not (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 2?
AError message
BNo output
C'Hello!'
D'init'
💡 Hint
Check the 'Output/Result' column at step 2 in the execution_table
At which step does WordPress run the hooked function?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
Look at the 'Action Taken' column to see when the function runs
If you use add_filter instead of add_action, what changes?
AThe function runs earlier
BThe hooked function can modify and return data
CThe function runs multiple times automatically
DNothing changes
💡 Hint
Refer to the concept_flow description about filters modifying data
Concept Snapshot
add_action('hook_name', 'function_name')
- Registers a function to run at a WordPress event
- Function runs when WordPress reaches that hook

add_filter('hook_name', 'function_name')
- Registers a function to modify data
- Function receives data, returns modified data

Hooks run in order of registration
Functions hooked do not run immediately
Full Transcript
In WordPress, add_action and add_filter let you connect your functions to specific points called hooks. add_action runs your function when WordPress hits an event, like 'init'. add_filter runs your function to change data passed through a filter. The process starts with WordPress loading and registering your hooks. When WordPress reaches the hook point, it runs all functions registered there. Actions run functions without changing data, filters run functions that receive and return data. For example, add_action('init', 'say_hello') registers say_hello to run at 'init'. When WordPress runs 'init', it calls say_hello, which prints 'Hello!'. This happens after registration, not immediately. Understanding when and how your hooked functions run helps you customize WordPress behavior safely and effectively.