Bird
Raised Fist0
Wordpressframework~20 mins

Filter hooks in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Filter Hook 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 filter hook modification?
Consider the following WordPress filter hook code. What will be the final output of apply_filters('greet', 'Hello')?
Wordpress
<?php
add_filter('greet', function($text) {
    return $text . ', friend!';
});
add_filter('greet', function($text) {
    return strtoupper($text);
});

echo apply_filters('greet', 'Hello');
?>
AHELLO
BHELLO, FRIEND!
CHello, friend!
DHello
Attempts:
2 left
💡 Hint
Remember that filters run in the order they are added, and each filter receives the output of the previous one.
📝 Syntax
intermediate
1:30remaining
Which option causes a syntax error in adding a filter?
Which of the following filter hook additions will cause a PHP syntax error?
Aadd_filter('title', function($title) { return strtoupper($title); });
Badd_filter('title', fn($title) => strtoupper($title));
Cadd_filter('title', function($title) { return strtoupper($title);
Dadd_filter('title', 'strtoupper');
Attempts:
2 left
💡 Hint
Check for missing punctuation or braces in the function syntax.
❓ state_output
advanced
2:00remaining
What is the value of $content after applying these filters?
Given the following code, what is the value of $content after apply_filters('content_filter', $content)?
Wordpress
<?php
$content = 'Start';
add_filter('content_filter', function($text) {
    return $text . ' + First';
});
add_filter('content_filter', function($text) {
    return $text . ' + Second';
}, 5);
$content = apply_filters('content_filter', $content);
?>
AStart + Second + First
BStart + First + Second
CStart + First
DStart + Second
Attempts:
2 left
💡 Hint
Lower priority numbers run earlier in WordPress filters.
🔧 Debug
advanced
1:30remaining
Which option causes a runtime error when applying this filter?
Given this filter hook, which option will cause a runtime error when apply_filters('number_filter', 10) is called?
Wordpress
<?php
add_filter('number_filter', function($num) {
    return $num * 2;
});
Aadd_filter('number_filter', function($num) { return $num + 5; });
Badd_filter('number_filter', function($num) { return $num / 2; });
Cadd_filter('number_filter', function($num) { return $num - 3; });
Dadd_filter('number_filter', function($num) { return $num->value; });
Attempts:
2 left
💡 Hint
Consider the type of the input and what operations are valid on it.
🧠 Conceptual
expert
2:30remaining
Which option best describes how filter hooks modify data in WordPress?
Select the option that correctly explains how filter hooks work in WordPress.
AFilters let you modify data by passing it through a chain of functions that each return a modified value.
BFilters allow you to replace core WordPress functions with your own implementations.
CFilters automatically cache data to improve WordPress performance without code changes.
DFilters are used to enqueue scripts and styles in WordPress themes and plugins.
Attempts:
2 left
💡 Hint
Think about how data flows through filters and how the output changes.

Practice

(1/5)
1. What is the main purpose of a filter hook in WordPress?
easy
A. To add new pages to the WordPress admin menu
B. To modify data before WordPress uses it
C. To create new database tables
D. To send emails automatically

Solution

  1. Step 1: Understand what filter hooks do

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

    Only To modify data before WordPress uses it describes modifying data before WordPress uses it, which matches the purpose of filter hooks.
  3. Final Answer:

    To modify data before WordPress uses it -> Option B
  4. Quick Check:

    Filter hooks = modify data before use [OK]
Hint: Filter hooks change data before WordPress uses it [OK]
Common Mistakes:
  • Confusing filter hooks with action hooks
  • Thinking filter hooks add new features instead of modifying data
  • Believing filter hooks create database tables
2. Which of the following is the correct way to add a filter hook in WordPress?
easy
A. add_filter('the_title', 'my_title_filter');
B. add_action('the_title', 'my_title_filter');
C. filter_add('the_title', 'my_title_filter');
D. add_filter('the_title', my_title_filter);

Solution

  1. Step 1: Identify the correct function name and syntax

    The correct function to add a filter hook is add_filter with the hook name as a string and the callback function name as a string.
  2. Step 2: Check each option for syntax correctness

    add_filter('the_title', 'my_title_filter'); uses add_filter with both parameters as strings, which is correct. add_action('the_title', 'my_title_filter'); uses add_action, which is for actions, not filters. filter_add('the_title', 'my_title_filter'); uses a wrong function name. add_filter('the_title', my_title_filter); misses quotes around the callback function name.
  3. Final Answer:

    add_filter('the_title', 'my_title_filter'); -> Option A
  4. Quick Check:

    Use add_filter with hook and callback as strings [OK]
Hint: Use add_filter('hook', 'function_name') with quotes [OK]
Common Mistakes:
  • Using add_action instead of add_filter
  • Not quoting the callback function name
  • Using incorrect function names like filter_add
3. What will be the output of this code snippet?
function add_exclamation($text) {
  return $text . '!';
}
add_filter('the_title', 'add_exclamation');
$title = apply_filters('the_title', 'Hello');
echo $title;
medium
A. Hello
B. Hello!!
C. Hello!
D. Error: undefined function

Solution

  1. Step 1: Understand the filter function and hook

    The function add_exclamation adds an exclamation mark to the text. It is hooked to 'the_title' filter.
  2. Step 2: Apply the filter to the string 'Hello'

    Calling apply_filters('the_title', 'Hello') runs the filter function, returning 'Hello!'.
  3. Final Answer:

    Hello! -> Option C
  4. Quick Check:

    Filter adds '!' to 'Hello' = 'Hello!' [OK]
Hint: apply_filters runs all filter functions on the value [OK]
Common Mistakes:
  • Expecting original text without modification
  • Thinking filter adds multiple exclamations
  • Confusing add_filter with add_action
4. Identify the error in this filter hook code:
function change_content($content) {
  echo strtoupper($content);
}
add_filter('the_content', 'change_content');
medium
A. The function should return the modified content, not echo it
B. The hook name 'the_content' is invalid
C. add_filter should be add_action here
D. The function name cannot be 'change_content'

Solution

  1. Step 1: Review the filter function requirements

    Filter functions must return the modified value so WordPress can use it.
  2. Step 2: Check the function implementation

    The function uses echo instead of returning the modified content, so WordPress receives no changed value.
  3. Final Answer:

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

    Filter functions must return values, not echo [OK]
Hint: Filter functions must return modified data, not print it [OK]
Common Mistakes:
  • Using echo instead of return in filter functions
  • Confusing filter hooks with action hooks
  • Assuming hook names are invalid without checking
5. You want to modify the excerpt text to add 'Read more...' only if the excerpt is not empty. Which filter hook function correctly does this?
function add_read_more($excerpt) {
  // Your code here
}
add_filter('get_the_excerpt', 'add_read_more');
hard
A. echo $excerpt . ' Read more...';
B. return $excerpt . ' Read more...';
C. if (!$excerpt) { return 'Read more...'; } else { return $excerpt; }
D. if ($excerpt) { return $excerpt . ' Read more...'; } else { return $excerpt; }

Solution

  1. Step 1: Understand the requirement

    We add ' Read more...' only if the excerpt is not empty (truthy).
  2. Step 2: Analyze each option's logic

    if ($excerpt) { return $excerpt . ' Read more...'; } else { return $excerpt; } checks if excerpt is truthy, then appends text and returns it; else returns original excerpt. return $excerpt . ' Read more...'; always appends text even if excerpt is empty. if (!$excerpt) { return 'Read more...'; } else { return $excerpt; } adds 'Read more...' only if excerpt is empty, which is opposite. echo $excerpt . ' Read more...'; uses echo instead of return, which is incorrect for filters.
  3. Final Answer:

    if ($excerpt) { return $excerpt . ' Read more...'; } else { return $excerpt; } -> Option D
  4. Quick Check:

    Return modified excerpt only if not empty [OK]
Hint: Return modified value only when excerpt is not empty [OK]
Common Mistakes:
  • Appending text without checking if excerpt is empty
  • Using echo instead of return in filter functions
  • Reversing the condition logic
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