What if you could add powerful features to WordPress without ever touching its core files?
Why Common action hooks in Wordpress? - Purpose & Use Cases
Imagine you want to add a custom message or feature to your WordPress site every time a post is published, but you have to edit core files or copy-paste code everywhere manually.
Manually changing core files or repeating code is risky, time-consuming, and makes updates a nightmare because your changes can be lost or cause errors.
Common action hooks let you 'hook into' WordPress events like publishing a post, so you can add your custom code cleanly without touching core files.
Edit core file to add custom code after post publish
add_action('publish_post', 'my_custom_function'); function my_custom_function() { // custom code }
Hooks enable you to extend WordPress easily and safely by running your code at the right moments without breaking anything.
Automatically send an email notification to subscribers whenever a new blog post goes live using the 'publish_post' action hook.
Manual edits to core files are risky and hard to maintain.
Action hooks let you add custom behavior at key events safely.
This makes your WordPress site flexible and easier to update.
Practice
Solution
Step 1: Understand the purpose of
Theinithookinithook runs after WordPress loads but before headers are sent, ideal for initialization tasks.Step 2: Compare with other hooks
wp_headandwp_footerrun during page rendering,save_postruns on post save, so they don't fit the timing.Final Answer:
init-> Option DQuick Check:
Initialization hook =init[OK]
init runs early after loading [OK]- Confusing
wp_headwithinit - Thinking
save_postruns on page load - Mixing
wp_footertiming withinit
my_custom_function to the wp_footer action hook?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 CQuick Check:
Function name as string, no parentheses [OK]
- Passing function name without quotes
- Calling the function with parentheses inside add_action
- Not quoting the hook name string
add_action('wp_head', function() { echo '<!-- Header comment -->'; });Solution
Step 1: Understand the
Thewp_headhook timingwp_headhook 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 BQuick Check:
wp_headoutputs inside <head> [OK]
wp_head outputs inside <head> tag [OK]- Thinking output appears in footer
- Believing anonymous functions are disallowed
- Assuming hook causes error
add_action('save_post', 'save_custom_data');
function save_custom_data() {
update_post_meta(get_the_ID(), 'custom_key', 'custom_value');
}Solution
Step 1: Check the save_post hook function signature
Thesave_posthook 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 AQuick Check:
save_post callback needs post ID parameter [OK]
- Ignoring the post ID parameter in callback
- Using wrong hook name
- Assuming get_the_ID() works inside save_post
Solution
Step 1: Identify the correct hook for footer scripts
Thewp_footerhook is the proper place to add scripts before the closing</body>tag.Step 2: Use conditional check for front page
Inside the callback, useis_front_page()to ensure the script runs only on the front page.Final Answer:
Useadd_action('wp_footer', 'my_script_function')and inside the function checkis_front_page()before outputting the script. -> Option AQuick Check:
Footer hook + front page check = Useadd_action('wp_footer', 'my_script_function')and inside the function checkis_front_page()before outputting the script. [OK]
- Using init hook to output scripts directly
- Adding scripts in wp_head without condition
- Using save_post hook for front page scripts
Ready to prove it?
Pass the test and this pattern moves unknown
