0
0
Wordpressframework~20 mins

add_action and add_filter in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WordPress Hooks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this add_action example?
Consider this WordPress code snippet. What will be printed on the page when the 'wp_footer' action runs?
Wordpress
<?php
function show_footer_message() {
    echo '<p>Footer message here</p>';
}
add_action('wp_footer', 'show_footer_message');
?>
ANothing is printed because 'wp_footer' is not a valid hook.
BThe text '<p>Footer message here</p>' appears at the bottom of the page.
CA PHP error occurs because the function is not hooked correctly.
DThe message appears at the top of the page instead of the footer.
Attempts:
2 left
💡 Hint
Think about when the 'wp_footer' action runs in WordPress page lifecycle.
state_output
intermediate
2:00remaining
What does this add_filter code output?
Given this WordPress filter code, what will be the final title displayed?
Wordpress
<?php
function add_prefix_to_title($title) {
    return 'Prefix: ' . $title;
}
add_filter('the_title', 'add_prefix_to_title');
// Later in template:
echo apply_filters('the_title', 'Hello World');
?>
APrefix: Hello World
BHello World
CPrefix:
DA PHP error because apply_filters is used incorrectly.
Attempts:
2 left
💡 Hint
Filters modify data passed through them before output.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in add_action usage?
Identify the code snippet that will cause a syntax error when hooking a function with add_action.
Aadd_action('init', 'my_function');
Badd_action('init', function() { echo 'Hi'; });
Cadd_action('init', 'my_function', 10);
Dadd_action('init', my_function);
Attempts:
2 left
💡 Hint
Check if the function name is quoted as a string.
🔧 Debug
advanced
2:00remaining
Why does this add_filter not change the output?
This code tries to change the content but the output remains unchanged. Why?
Wordpress
<?php
function change_content($content) {
    $content = 'Changed content';
    return $content;
}
add_filter('the_content', 'change_content');
?>
AThe function name is not registered correctly in add_filter.
BThe filter hook 'the_content' is misspelled.
CThe function does not return the modified content, so the filter has no effect.
DThe filter priority is too low to affect the content.
Attempts:
2 left
💡 Hint
Filters must return the modified value to work.
🧠 Conceptual
expert
2:00remaining
Which statement correctly explains the difference between add_action and add_filter?
Choose the option that best describes how add_action and add_filter differ in WordPress.
Aadd_action hooks run functions at specific points without modifying data; add_filter hooks modify data by returning a value.
Badd_action hooks let you modify data and must return a value; add_filter hooks only run code without returning anything.
Cadd_action and add_filter are interchangeable and behave the same way in WordPress.
Dadd_action hooks are used only for admin pages; add_filter hooks are used only for front-end pages.
Attempts:
2 left
💡 Hint
Think about whether the hooked function changes data or just runs code.