0
0
Wordpressframework~10 mins

Hook priority and arguments in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hook priority and arguments
Add Hook with Priority
WordPress Stores Hook
Trigger Hook Event
WordPress Sorts Hooks by Priority
Call Hook Functions in Order
Pass Arguments to Hook Functions
Functions Execute with Arguments
WordPress stores hooks with their priority and arguments, then calls them in order passing the arguments when the hook runs.
Execution Sample
Wordpress
<?php
add_action('init', 'first_hook', 10, 2);
add_action('init', 'second_hook', 5, 1);
function first_hook($arg1, $arg2) { echo "First: $arg1, $arg2"; }
function second_hook($arg1) { echo "Second: $arg1"; }
do_action('init', 'hello', 'world');
?>
This code adds two functions to the 'init' hook with different priorities and argument counts, then triggers the hook with two arguments.
Execution Table
StepActionHook NamePriorityArguments PassedFunction CalledOutput
1Add Hookinit102first_hooknone
2Add Hookinit51second_hooknone
3Trigger HookinitN/A['hello', 'world']N/Anone
4Sort Hooks by Priorityinit5 then 10N/AN/Anone
5Call Hook Functioninit5['hello']second_hookSecond: hello
6Call Hook Functioninit10['hello', 'world']first_hookFirst: hello, world
7EndinitN/AN/AN/AAll hooks called
💡 All hooks for 'init' called in priority order; lower number runs first.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 6Final
Hooks['init']empty[{func: first_hook, priority:10, args:2}][{func: first_hook, priority:10, args:2}, {func: second_hook, priority:5, args:1}][{func: second_hook, priority:5, args:1}, {func: first_hook, priority:10, args:2}]N/AN/A
Arguments PassedN/AN/AN/AN/A['hello', 'world']N/A
Key Moments - 3 Insights
Why does 'second_hook' run before 'first_hook' even though it was added later?
Because WordPress runs hooks in order of priority number, lower numbers run first. 'second_hook' has priority 5, which is before 10 of 'first_hook' (see execution_table steps 4 and 5).
Why does 'second_hook' only get one argument even though two were passed?
Because when adding the hook, it was set to accept only 1 argument. WordPress passes only as many arguments as the hook expects (see execution_table step 5).
What happens if two hooks have the same priority?
They run in the order they were added. WordPress sorts by priority first, then by order added (not shown in this example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which hook runs first when 'init' is triggered?
Afirst_hook
Bsecond_hook
Cboth run simultaneously
Dnone run
💡 Hint
Check step 4 and 5 where hooks are sorted and called by priority.
At which step does WordPress sort hooks by priority?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Look for the step mentioning sorting hooks by priority.
If 'first_hook' was added with priority 3, how would the execution order change?
AThey run in the order added
Bsecond_hook runs before first_hook
Cfirst_hook runs before second_hook
DNeither runs
💡 Hint
Lower priority number means earlier execution (see concept_flow and execution_table).
Concept Snapshot
WordPress hooks run in order of priority (lower number first).
Add hooks with add_action or add_filter specifying priority and argument count.
When hook triggers, WordPress sorts hooks by priority.
Each hook function receives only the number of arguments it expects.
Hooks with same priority run in order added.
Full Transcript
In WordPress, hooks are functions attached to events. When you add a hook, you specify its priority and how many arguments it accepts. WordPress stores these hooks and when the event triggers, it sorts hooks by priority number, running lower numbers first. Then it calls each hook function, passing the arguments given by the event, but only as many as the hook expects. This way, you control the order and data each hook receives. For example, if you add two hooks to 'init' with priorities 10 and 5, the one with priority 5 runs first. If the event passes two arguments but a hook expects only one, it gets just one. Hooks with the same priority run in the order they were added.