Challenge - 5 Problems
Filter Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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');
?>Attempts:
2 left
💡 Hint
Remember that filters modify the value passed through them.
✗ Incorrect
The filter 'the_title' is hooked with a function that adds 'Prefix: ' before the title. When apply_filters is called, it returns the modified string.
❓ component_behavior
intermediate2: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');
?>Attempts:
2 left
💡 Hint
Consider what happens when a filter is removed before applying it.
✗ Incorrect
The filter is added and then immediately removed, so when apply_filters runs, no modification happens.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Remember the order of parameters in add_filter is: hook, callback, priority, accepted_args.
✗ Incorrect
The correct order is hook name, callback function, priority number, and number of accepted arguments.
🔧 Debug
advanced2:00remaining
Why does this filter not modify the output?
Consider this code snippet:
Why does the output remain 'Hello' instead of 'HELLO'?
Attempts:
2 left
💡 Hint
Filters must return the modified value to change output.
✗ Incorrect
The function changes the variable but does not return it, so the filter returns null and original value is used.
🧠 Conceptual
expert3: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');
?>Attempts:
2 left
💡 Hint
Lower priority numbers run earlier in the filter chain.
✗ Incorrect
Filters with lower priority run first, so second_filter runs before first_filter, appending its text first.