Bird
Raised Fist0
Wordpressframework~20 mins

Common action hooks 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 happens when the 'init' action hook is triggered?
In WordPress, the 'init' action hook is one of the earliest hooks triggered during page load. What is the typical behavior when this hook runs?
AIt runs after WordPress has finished loading but before any headers are sent, allowing you to register custom post types and taxonomies.
BIt runs after the theme template is loaded and outputs the page content.
CIt runs only when a user logs in and initializes user session data.
DIt runs after the footer is rendered to enqueue scripts for the next page.
Attempts:
2 left
💡 Hint
Think about when you want to register custom post types or taxonomies in WordPress.
❓ state_output
intermediate
2:00remaining
What is the output of this code using 'wp_footer' hook?
Consider this WordPress code snippet added to functions.php:
add_action('wp_footer', function() { echo '

Footer message

'; });

What will appear on the page?
Wordpress
add_action('wp_footer', function() { echo '<p>Footer message</p>'; });
AThe text will appear at the top of the page before the header.
BThe text '<p>Footer message</p>' will appear just before the closing </body> tag in the HTML.
CNothing will appear because 'wp_footer' is not a valid hook.
DThe text will appear inside the <head> section of the HTML.
Attempts:
2 left
💡 Hint
Remember where the 'wp_footer' hook is placed in the theme template.
📝 Syntax
advanced
2:00remaining
Which option correctly adds a function to the 'admin_menu' action hook?
You want to add a custom admin menu page in WordPress. Which code snippet correctly hooks your function to 'admin_menu'?
Aadd_action('admin_menu', function my_custom_menu() { add_menu_page('Title', 'Menu', 'manage_options', 'slug', 'callback'); });
Badd_action('admin_menu', my_custom_menu()); function my_custom_menu() { add_menu_page('Title', 'Menu', 'manage_options', 'slug', 'callback'); }
C} ;)'kcabllac' ,'guls' ,'snoitpo_eganam' ,'uneM' ,'eltiT'(egap_unem_dda { )(unem_motsuc_ym noitcnuf ;)'unem_motsuc_ym' ,'unem_nimda'(noitca_dda
Dadd_action('admin_menu', 'my_custom_menu'); function my_custom_menu() { add_menu_page('Title', 'Menu', 'manage_options', 'slug', 'callback'); }
Attempts:
2 left
💡 Hint
Check how the function name is passed to add_action and if parentheses are used.
🔧 Debug
advanced
2:00remaining
Why does this 'save_post' hook not trigger the function?
Given this code in functions.php:
add_action('save_post', 'save_custom_data');
function save_custom_data() {
  error_log('Saving data');
}

Why might 'Saving data' not appear in the error log when a post is saved?
Wordpress
add_action('save_post', 'save_custom_data');
function save_custom_data() {
  error_log('Saving data');
}
AThe function name is misspelled in the add_action call.
BThe error_log function is disabled in the server configuration.
CThe 'save_post' hook passes parameters, but the function does not accept any, causing it not to run.
DThe 'save_post' hook only triggers on pages, not posts.
Attempts:
2 left
💡 Hint
Check the function signature and parameters expected by the hook.
🧠 Conceptual
expert
2:00remaining
Which hook is best to enqueue scripts and styles properly in WordPress?
You want to add custom JavaScript and CSS files to your WordPress theme or plugin. Which action hook should you use to enqueue these assets correctly?
Awp_enqueue_scripts
Binit
Ctemplate_redirect
Dwp_head
Attempts:
2 left
💡 Hint
Think about the hook designed specifically for adding scripts and styles on the front end.

Practice

(1/5)
1. Which WordPress action hook is commonly used to run code after WordPress has finished loading but before any headers are sent?
easy
A. wp_footer
B. wp_head
C. save_post
D. init

Solution

  1. Step 1: Understand the purpose of init hook

    The init hook runs after WordPress loads but before headers are sent, ideal for initialization tasks.
  2. Step 2: Compare with other hooks

    wp_head and wp_footer run during page rendering, save_post runs on post save, so they don't fit the timing.
  3. Final Answer:

    init -> Option D
  4. Quick Check:

    Initialization hook = init [OK]
Hint: Remember: init runs early after loading [OK]
Common Mistakes:
  • Confusing wp_head with init
  • Thinking save_post runs on page load
  • Mixing wp_footer timing with init
2. Which of the following is the correct syntax to add a function named my_custom_function to the wp_footer action hook?
easy
A. add_action('wp_footer', my_custom_function);
B. add_action(wp_footer, 'my_custom_function');
C. add_action('wp_footer', 'my_custom_function');
D. add_action('wp_footer', my_custom_function());

Solution

  1. Step 1: Check the function name parameter

    The function name must be passed as a string, so quotes are required around 'my_custom_function'.
  2. Step 2: Verify the hook name and function call

    The hook name must be a string, so 'wp_footer' is correct. The function should not be called (no parentheses).
  3. Final Answer:

    add_action('wp_footer', 'my_custom_function'); -> Option C
  4. Quick Check:

    Function name as string, no parentheses [OK]
Hint: Function names in add_action must be quoted strings [OK]
Common Mistakes:
  • Passing function name without quotes
  • Calling the function with parentheses inside add_action
  • Not quoting the hook name string
3. What will be the output when the following code runs in a WordPress theme?
add_action('wp_head', function() { echo '<!-- Header comment -->'; });
medium
A. The code will cause a fatal error due to incorrect hook usage.
B. The comment <!-- Header comment --> will appear inside the HTML <head> section.
C. The comment will not appear because anonymous functions are not allowed.
D. The comment will appear at the bottom of the page before </body>.

Solution

  1. Step 1: Understand the wp_head hook timing

    The wp_head hook runs inside the HTML <head> section of the page.
  2. Step 2: Analyze the anonymous function output

    The function echoes an HTML comment, so it will be output exactly where the hook runs, inside <head>.
  3. Final Answer:

    The comment <!-- Header comment --> will appear inside the HTML <head> section. -> Option B
  4. Quick Check:

    wp_head outputs inside <head> [OK]
Hint: Remember wp_head outputs inside <head> tag [OK]
Common Mistakes:
  • Thinking output appears in footer
  • Believing anonymous functions are disallowed
  • Assuming hook causes error
4. Identify the error in this code snippet that tries to save custom data when a post is saved:
add_action('save_post', 'save_custom_data');
function save_custom_data() {
  update_post_meta(get_the_ID(), 'custom_key', 'custom_value');
}
medium
A. The function save_custom_data should accept the post ID parameter.
B. The hook name should be 'save_post_data' instead of 'save_post'.
C. update_post_meta cannot be used inside save_post hook.
D. get_the_ID() returns the wrong post ID inside save_post.

Solution

  1. Step 1: Check the save_post hook function signature

    The save_post hook passes the post ID as a parameter to the callback function, so the function must accept it.
  2. Step 2: Analyze the function usage of get_the_ID()

    get_the_ID() may not return the correct post ID in this context; the passed post ID parameter should be used instead.
  3. Final Answer:

    The function save_custom_data should accept the post ID parameter. -> Option A
  4. Quick Check:

    save_post callback needs post ID parameter [OK]
Hint: save_post callback must accept post ID parameter [OK]
Common Mistakes:
  • Ignoring the post ID parameter in callback
  • Using wrong hook name
  • Assuming get_the_ID() works inside save_post
5. You want to add a custom script only on the front page footer using WordPress hooks. Which combination of hooks and conditions is the best approach?
hard
A. Use add_action('wp_footer', 'my_script_function') and inside the function check is_front_page() before outputting the script.
B. Use add_action('init', 'my_script_function') and output the script directly.
C. Use add_action('wp_head', 'my_script_function') without any condition.
D. Use add_action('save_post', 'my_script_function') to add the script on front page.

Solution

  1. Step 1: Identify the correct hook for footer scripts

    The wp_footer hook is the proper place to add scripts before the closing </body> tag.
  2. Step 2: Use conditional check for front page

    Inside the callback, use is_front_page() to ensure the script runs only on the front page.
  3. Final Answer:

    Use add_action('wp_footer', 'my_script_function') and inside the function check is_front_page() before outputting the script. -> Option A
  4. Quick Check:

    Footer hook + front page check = Use add_action('wp_footer', 'my_script_function') and inside the function check is_front_page() before outputting the script. [OK]
Hint: Add scripts in wp_footer with is_front_page() check [OK]
Common Mistakes:
  • Using init hook to output scripts directly
  • Adding scripts in wp_head without condition
  • Using save_post hook for front page scripts
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