An action hook is a way to add or change WordPress behavior by running custom code at specific points during WordPress execution.
Click to reveal answer
beginner
What does the wp_head action hook do?
The wp_head hook runs in the <head> section of a WordPress theme. It lets you add scripts, styles, or meta tags before the page loads.
Click to reveal answer
intermediate
When is the init action hook triggered?
The init hook runs after WordPress has loaded but before any headers are sent. It's used to initialize plugins, register post types, or start sessions.
Click to reveal answer
beginner
What is the purpose of the wp_footer action hook?
The wp_footer hook runs just before the closing </body> tag. It is used to add scripts or code that should load at the end of the page.
Click to reveal answer
beginner
How do you add a function to an action hook in WordPress?
Use the add_action('hook_name', 'your_function') function. This tells WordPress to run your function when the hook runs.
Click to reveal answer
Which hook is best to add custom code inside the <head> section of a WordPress theme?
Aadmin_init
Bwp_head
Cwp_footer
Dinit
✗ Incorrect
The wp_head hook runs inside the <head> section, perfect for adding styles or scripts.
When does the init action hook run?
AAfter WordPress loads but before headers
BBefore WordPress loads
CAfter headers are sent
DAt the end of the page
✗ Incorrect
The init hook runs after WordPress loads but before headers are sent.
Which hook is used to add code just before the closing </body> tag?
Ainit
Bwp_head
Cwp_footer
Dtemplate_redirect
✗ Incorrect
The wp_footer hook runs just before the closing </body> tag.
How do you attach your function to a WordPress action hook?
Aadd_action('hook', 'function')
Badd_filter('hook', 'function')
Cdo_action('hook', 'function')
Dregister_hook('hook', 'function')
✗ Incorrect
Use add_action to attach your function to an action hook.
Which hook is NOT typically used for front-end page modifications?
Awp_head
Bwp_footer
Cinit
Dadmin_init
✗ Incorrect
admin_init runs in the admin dashboard, not on the front-end.
Explain what an action hook is in WordPress and give two examples of common action hooks used in themes.
Think about points in page load where you can add code.
You got /3 concepts.
Describe how you would use the init action hook in a plugin.
Init runs early, before headers.
You got /3 concepts.
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
Step 1: Understand the purpose of init hook
The init hook runs after WordPress loads but before headers are sent, ideal for initialization tasks.
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.
Final Answer:
init -> Option D
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
Step 1: Check the function name parameter
The function name must be passed as a string, so quotes are required around 'my_custom_function'.
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).
Final Answer:
add_action('wp_footer', 'my_custom_function'); -> Option C
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?
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
Step 1: Understand the wp_head hook timing
The wp_head hook runs inside the HTML <head> section of the page.
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>.
Final Answer:
The comment <!-- Header comment --> will appear inside the HTML <head> section. -> Option B
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
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.
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.
Final Answer:
The function save_custom_data should accept the post ID parameter. -> Option A
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
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.
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.
Final Answer:
Use add_action('wp_footer', 'my_script_function') and inside the function check is_front_page() before outputting the script. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.