0
0
Wordpressframework~20 mins

Common filter hooks in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Filter Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a filter modifying post title
What will be the output of the following WordPress code snippet when the post title is 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
Cthe_title
DPrefix: Hello World
Attempts:
2 left
💡 Hint
Remember that filters modify the value passed through them.
component_behavior
intermediate
2:00remaining
Effect of removing a filter hook
Given the following code, what will be the output of the post content?
Wordpress
<?php
function add_suffix_to_content($content) {
    return $content . ' - Suffix';
}
add_filter('the_content', 'add_suffix_to_content');
remove_filter('the_content', 'add_suffix_to_content');
echo apply_filters('the_content', 'Sample content');
?>
ASample content
BSample content - Suffix - Suffix
C - Suffix
DSample content - Suffix
Attempts:
2 left
💡 Hint
Consider what happens when a filter is removed before applying it.
📝 Syntax
advanced
2:00remaining
Correct syntax to add a filter with priority
Which option correctly adds a filter to 'the_excerpt' with a priority of 15 and accepts 2 arguments?
Aadd_filter('the_excerpt', 'modify_excerpt', 2, 15);
Badd_filter('the_excerpt', 'modify_excerpt', 15, 2);
Cadd_filter('the_excerpt', 'modify_excerpt', 15);
Dadd_filter('the_excerpt', 15, 'modify_excerpt', 2);
Attempts:
2 left
💡 Hint
Remember the order of parameters in add_filter is: hook, callback, priority, accepted_args.
🔧 Debug
advanced
2:00remaining
Why does this filter not modify the output?
Consider this code snippet: Why does the output remain 'Hello' instead of 'HELLO'?
Aapply_filters is not the correct function to use.
BThe filter hook name 'the_title' is incorrect.
CThe function does not return the modified title.
DThe function should use echo instead of return.
Attempts:
2 left
💡 Hint
Filters must return the modified value to change output.
🧠 Conceptual
expert
3:00remaining
Understanding filter hook execution order
If two filters are added to 'the_content' hook as follows: add_filter('the_content', 'first_filter', 20); add_filter('the_content', 'second_filter', 10); And both functions append their name to the content string, what will be the output of apply_filters('the_content', 'Text')?
Wordpress
<?php
function first_filter($content) {
    return $content . ' first_filter';
}
function second_filter($content) {
    return $content . ' second_filter';
}
add_filter('the_content', 'first_filter', 20);
add_filter('the_content', 'second_filter', 10);
echo apply_filters('the_content', 'Text');
?>
AText second_filter first_filter
BText second_filter
CText first_filter second_filter
DText first_filter
Attempts:
2 left
💡 Hint
Lower priority numbers run earlier in the filter chain.