Bird
Raised Fist0
Wordpressframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the priority number control when adding a function to a WordPress hook using add_action or add_filter?
easy
A. The order in which functions run on the same hook
B. The number of arguments the function accepts
C. The type of hook (action or filter)
D. The name of the hook to attach to

Solution

  1. Step 1: Understand the role of priority in hooks

    Priority determines the sequence functions run when multiple functions are attached to the same hook.
  2. Step 2: Differentiate priority from other parameters

    Priority is not about argument count or hook type; it only controls execution order.
  3. Final Answer:

    The order in which functions run on the same hook -> Option A
  4. Quick Check:

    Priority controls execution order = B [OK]
Hint: Priority sets function run order on hooks [OK]
Common Mistakes:
  • Confusing priority with argument count
  • Thinking priority changes hook type
  • Assuming priority sets hook name
2. Which of the following is the correct syntax to add a function my_function to the init action hook with priority 15 and accepting 2 arguments?
easy
A. add_action('init', 'my_function', 2);
B. add_action('init', 'my_function', 2, 15);
C. add_action('init', my_function, 15, 2);
D. add_action('init', 'my_function', 15, 2);

Solution

  1. Step 1: Recall the parameter order of add_action

    The order is hook name, function name (string), priority (default 10), and accepted arguments (default 1).
  2. Step 2: Match parameters to the question

    Priority is 15, accepted arguments is 2, so the call is add_action('init', 'my_function', 15, 2);
  3. Final Answer:

    add_action('init', 'my_function', 15, 2); -> Option D
  4. Quick Check:

    Priority=15, args=2 syntax = D [OK]
Hint: Remember add_action(hook, func, priority, args) order [OK]
Common Mistakes:
  • Swapping priority and argument count
  • Omitting quotes around function name
  • Passing wrong number of parameters
3. Given the code below, what will be the output?
function first_func($arg) {
  return $arg . ' first';
}
function second_func($arg) {
  return $arg . ' second';
}
add_filter('test_hook', 'first_func', 20, 1);
add_filter('test_hook', 'second_func', 10, 1);
$result = apply_filters('test_hook', 'Run');
echo $result;
medium
A. Run second
B. Run first second
C. Run second first
D. Run first

Solution

  1. Step 1: Understand filter execution order by priority

    Lower priority numbers run first. Here, second_func has priority 10, first_func has 20.
  2. Step 2: Trace filter application

    Input 'Run' goes to second_func first, returning 'Run second'. Then 'Run second' goes to first_func, returning 'Run second first'.
  3. Final Answer:

    Run second first -> Option C
  4. Quick Check:

    Lower priority runs first = A [OK]
Hint: Lower priority number runs first in filters [OK]
Common Mistakes:
  • Assuming higher priority runs first
  • Ignoring filter chaining order
  • Confusing actions and filters
4. What is wrong with this code snippet?
function my_filter($a, $b) {
  return $a + $b;
}
add_filter('calc', 'my_filter', 10, 1);
$result = apply_filters('calc', 5, 10);
echo $result;
medium
A. The priority number 10 is invalid
B. The function expects 2 arguments but only 1 is accepted in add_filter
C. apply_filters cannot pass more than one argument
D. Function name should be in double quotes

Solution

  1. Step 1: Check function parameter count vs accepted args

    Function my_filter expects 2 arguments, but add_filter specifies only 1 argument accepted.
  2. Step 2: Understand consequence of argument mismatch

    Only 1 argument will be passed, so $b will be undefined, causing incorrect behavior or error.
  3. Final Answer:

    The function expects 2 arguments but only 1 is accepted in add_filter -> Option B
  4. Quick Check:

    Accepted args must match function params = A [OK]
Hint: Match accepted args to function parameters [OK]
Common Mistakes:
  • Ignoring argument count mismatch
  • Thinking priority 10 is invalid
  • Believing apply_filters limits arguments
5. You want to add two functions to the same filter hook modify_text. The first function should run before the second and both accept 3 arguments. Which is the correct way to add them?
hard
A. add_filter('modify_text', 'first_func', 5, 3); add_filter('modify_text', 'second_func', 10, 3);
B. add_filter('modify_text', 'first_func', 10, 3); add_filter('modify_text', 'second_func', 5, 3);
C. add_filter('modify_text', 'first_func', 3, 5); add_filter('modify_text', 'second_func', 3, 10);
D. add_filter('modify_text', 'first_func', 3); add_filter('modify_text', 'second_func', 3);

Solution

  1. Step 1: Determine priority order for execution

    Lower priority number runs first, so first_func should have lower priority than second_func.
  2. Step 2: Set accepted arguments correctly

    Both functions accept 3 arguments, so the fourth parameter in add_filter must be 3 for both.
  3. Final Answer:

    add_filter('modify_text', 'first_func', 5, 3); add_filter('modify_text', 'second_func', 10, 3); -> Option A
  4. Quick Check:

    Lower priority runs first, args=3 = C [OK]
Hint: Lower priority runs first; set args count correctly [OK]
Common Mistakes:
  • Swapping priority numbers
  • Setting wrong accepted argument count
  • Omitting accepted arguments parameter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard