0
0
Wordpressframework~20 mins

Hook priority and arguments in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hook Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output order of hooks with different priorities?
Consider these WordPress hooks added to the same action 'init' with different priorities. What will be the order of output when the 'init' action runs?
Wordpress
<?php
add_action('init', function() { echo 'First'; }, 10);
add_action('init', function() { echo 'Second'; }, 5);
add_action('init', function() { echo 'Third'; }, 15);
do_action('init');
?>
ASecondThirdFirst
BSecondFirstThird
CThirdSecondFirst
DFirstSecondThird
Attempts:
2 left
💡 Hint
Lower priority numbers run earlier in WordPress hooks.
state_output
intermediate
2:00remaining
How many arguments does the hooked function receive?
Given this code, how many arguments will the function 'my_callback' receive when the 'save_post' action runs?
Wordpress
<?php
function my_callback($post_id, $post, $update) {
    // ...
}
add_action('save_post', 'my_callback', 10, 3);
do_action('save_post', 123, (object) ['ID' => 123], true);
?>
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
The fourth parameter in add_action controls how many arguments are passed.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in hook registration?
Which of these add_action calls will cause a syntax error?
Aadd_action('init', 'my_function', 10);
Badd_action('init', 'my_function', 10, 2);
Cadd_action('init', 'my_function' 10, 2);
Dadd_action('init', 'my_function');
Attempts:
2 left
💡 Hint
Check for missing commas between arguments.
🔧 Debug
advanced
2:00remaining
Why does the hooked function not receive all arguments?
Given this code, why does 'my_callback' only receive 1 argument instead of 3?
Wordpress
<?php
function my_callback($a, $b, $c) {
    var_dump(func_get_args());
}
add_action('some_hook', 'my_callback', 10, 1);
do_action('some_hook', 1, 2, 3);
?>
ABecause add_action specifies only 1 argument to pass
BBecause do_action only passes 1 argument
CBecause the function definition is incorrect
DBecause the hook name is wrong
Attempts:
2 left
💡 Hint
Check the fourth parameter of add_action.
🧠 Conceptual
expert
3:00remaining
What happens if two hooks have the same priority and modify the same data?
If two functions hooked to the same filter have the same priority and both modify the input data, what determines which modification applies first?
AWordPress merges the modifications automatically
BThe hook added last runs first, so its modification applies first
CHooks with the same priority run in random order
DThe hook added first runs first, so its modification applies first
Attempts:
2 left
💡 Hint
Think about how WordPress stores hooks internally.