Bird
Raised Fist0
Wordpressframework~20 mins

Common filter hooks in Wordpress - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of a filter hook in WordPress?
easy
A. To enqueue scripts and styles
B. To add new pages to the WordPress admin menu
C. To create new database tables
D. To modify data before it is displayed or saved

Solution

  1. Step 1: Understand what filter hooks do

    Filter hooks allow you to change or modify data before WordPress uses or shows it.
  2. Step 2: Compare options to the definition

    Only To modify data before it is displayed or saved describes modifying data before display or saving, which matches filter hooks.
  3. Final Answer:

    To modify data before it is displayed or saved -> Option D
  4. Quick Check:

    Filter hooks modify data = D [OK]
Hint: Filter hooks change data before use or display [OK]
Common Mistakes:
  • Confusing filter hooks with action hooks
  • Thinking filters add new features instead of modifying data
  • Mixing up filters with script loading
2. Which of the following is the correct syntax to add a filter in WordPress?
easy
A. add_action('the_content', 'my_function');
B. add_filter('the_content', 'my_function');
C. apply_filter('the_content', 'my_function');
D. filter_add('the_content', 'my_function');

Solution

  1. Step 1: Recall the correct function to add a filter

    The WordPress function to add a filter is add_filter.
  2. Step 2: Check the syntax of each option

    add_filter('the_content', 'my_function'); uses add_filter with correct parameters. Others use wrong function names.
  3. Final Answer:

    add_filter('the_content', 'my_function'); -> Option B
  4. Quick Check:

    Use add_filter() to add filters = A [OK]
Hint: Use add_filter() to attach filter functions [OK]
Common Mistakes:
  • Using add_action instead of add_filter
  • Using non-existent functions like apply_filter
  • Mixing up function names
3. What will be the output of this code snippet?
add_filter('the_title', function($title) {
  return strtoupper($title);
});

// Later WordPress calls apply_filters('the_title', 'Hello World');
medium
A. "HELLO WORLD"
B. "hello world"
C. "Hello World"
D. Error: Function not found

Solution

  1. Step 1: Understand the filter function

    The filter changes the title by converting it to uppercase using strtoupper.
  2. Step 2: Apply the filter to the input string

    When WordPress applies the filter to 'Hello World', it returns 'HELLO WORLD'.
  3. Final Answer:

    "HELLO WORLD" -> Option A
  4. Quick Check:

    Filter changes title to uppercase = C [OK]
Hint: Filter returns modified data, here uppercase string [OK]
Common Mistakes:
  • Assuming original string is unchanged
  • Confusing add_filter with add_action
  • Expecting lowercase instead of uppercase
4. Identify the error in this filter hook code:
add_filter('the_content', 'modify_content');

function modify_content($content) {
  echo strtoupper($content);
}
medium
A. The filter name 'the_content' is incorrect
B. The function name must start with 'filter_'
C. The function should return the modified content, not echo it
D. add_filter requires three parameters

Solution

  1. Step 1: Review the filter function behavior

    Filter functions must return the modified data, not output it directly.
  2. Step 2: Check the code for return vs echo

    The function uses echo instead of return, so WordPress won't get the modified content.
  3. Final Answer:

    The function should return the modified content, not echo it -> Option C
  4. Quick Check:

    Filter functions must return data = B [OK]
Hint: Filter functions must return modified data, not print it [OK]
Common Mistakes:
  • Using echo instead of return in filter functions
  • Thinking filter names are arbitrary
  • Believing add_filter always needs three parameters
5. You want to add a filter that appends " - Powered by WordPress" to every post title but only if the title is not empty. Which code correctly implements this?
hard
A. add_filter('the_title', function($title) { if ($title !== '') { return $title . ' - Powered by WordPress'; } return $title; });
B. add_filter('the_title', function($title) { if ($title) { echo $title . ' - Powered by WordPress'; } });
C. add_filter('the_title', function() { return ' - Powered by WordPress'; });
D. add_filter('the_title', function($title) { return $title . ' - Powered by WordPress'; });

Solution

  1. Step 1: Check the condition for empty title

    add_filter('the_title', function($title) { if ($title !== '') { return $title . ' - Powered by WordPress'; } return $title; }); checks if the title is not empty before appending the text, which matches the requirement.
  2. Step 2: Verify the function returns the modified title

    add_filter('the_title', function($title) { if ($title !== '') { return $title . ' - Powered by WordPress'; } return $title; }); returns the modified title correctly; add_filter('the_title', function($title) { if ($title) { echo $title . ' - Powered by WordPress'; } }); uses echo which is wrong, C ignores the original title, D appends text even if title is empty.
  3. Final Answer:

    code with condition and return -> Option A
  4. Quick Check:

    Check empty title and return modified string = A [OK]
Hint: Return modified title only if not empty, use return not echo [OK]
Common Mistakes:
  • Using echo instead of return
  • Not checking if title is empty
  • Replacing title instead of appending
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