Bird
Raised Fist0
Wordpressframework~20 mins

add_action and add_filter in Wordpress - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of add_action in WordPress?
easy
A. To create new user roles
B. To change the content of posts before display
C. To register new database tables
D. To run custom code at specific WordPress events

Solution

  1. Step 1: Understand what add_action does

    add_action hooks your custom function to WordPress events so it runs at the right time.
  2. Step 2: Compare with other options

    Changing content before display is for filters, not actions. Registering tables or roles is unrelated to add_action.
  3. Final Answer:

    To run custom code at specific WordPress events -> Option D
  4. Quick Check:

    add_action runs code on events = D [OK]
Hint: Actions run code at events; filters change data [OK]
Common Mistakes:
  • Confusing add_action with add_filter
  • Thinking add_action changes data directly
  • Assuming add_action registers database or roles
2. Which of the following is the correct syntax to add a filter in WordPress?
easy
A. add_filter(my_function, 'the_content');
B. add_action('the_content', 'my_function');
C. add_filter('the_content', 'my_function');
D. add_action(my_function, 'the_content');

Solution

  1. Step 1: Recall the correct order of parameters for add_filter

    The first parameter is the filter hook name as a string, the second is the callback function name.
  2. Step 2: Check each option's parameter order and function used

    add_filter('the_content', 'my_function'); uses add_filter with correct parameter order. Options B and D use add_action incorrectly. add_filter(my_function, 'the_content'); reverses parameters.
  3. Final Answer:

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

    Filter syntax: add_filter(hook, function) = A [OK]
Hint: Filter syntax: hook name first, function second [OK]
Common Mistakes:
  • Swapping parameters order
  • Using add_action instead of add_filter
  • Passing function name before hook name
3. Given this code snippet:
function change_title($title) {
  return 'Hello: ' . $title;
}
add_filter('the_title', 'change_title');

What will be the output of the_title() for a post titled "World"?
medium
A. Hello: World
B. the_title
C. World
D. Hello World

Solution

  1. Step 1: Understand the filter function

    The function change_title adds "Hello: " before the original title.
  2. Step 2: Apply the filter to the post title

    When the_title() runs, WordPress applies the filter, so "World" becomes "Hello: World".
  3. Final Answer:

    Hello: World -> Option A
  4. Quick Check:

    Filter prepends 'Hello: ' to title = C [OK]
Hint: Filters modify data output; here title gets prefix [OK]
Common Mistakes:
  • Ignoring the filter effect on output
  • Confusing add_action with add_filter effect
  • Missing the colon and space after Hello
4. What is wrong with this code?
add_filter('the_content', 'modify_content');
function modify_content() {
  return 'Modified content';
}
medium
A. The hook name is incorrect
B. The callback function must accept the content parameter
C. add_filter should be add_action here
D. The function must echo content, not return

Solution

  1. Step 1: Check the callback function signature

    Filter callbacks must accept the original data as a parameter to modify it.
  2. Step 2: Identify the missing parameter

    The function modify_content has no parameters, so it cannot receive the content to change.
  3. Final Answer:

    The callback function must accept the content parameter -> Option B
  4. Quick Check:

    Filter callbacks need input parameter = B [OK]
Hint: Filter functions must accept and return data [OK]
Common Mistakes:
  • Omitting the parameter in filter callback
  • Using add_action instead of add_filter
  • Trying to echo instead of return in filter
5. You want to add a custom message after every post content but only for logged-in users. Which is the best way to do this using add_filter?
hard
A. Use add_filter('the_content', 'add_message'); and inside add_message check if user is logged in before appending message
B. Use add_action('the_content', 'add_message'); and append message without checking user
C. Use add_filter('the_content', 'add_message'); and always append message without user check
D. Use add_action('wp_footer', 'add_message'); to add message after content

Solution

  1. Step 1: Choose the correct hook and method

    To modify post content, add_filter('the_content', ...) is correct because it changes content before display.
  2. Step 2: Add user check inside callback

    To show message only to logged-in users, check user status inside the callback function before appending the message.
  3. Final Answer:

    Use add_filter('the_content', 'add_message'); and inside add_message check if user is logged in before appending message -> Option A
  4. Quick Check:

    Filter content + user check = A [OK]
Hint: Filter content and check user inside function [OK]
Common Mistakes:
  • Using add_action instead of add_filter for content change
  • Not checking user login status
  • Adding message in footer instead of content
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