0
0
Wordpressframework~10 mins

Common action hooks in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common action hooks
WordPress loads core
Trigger action hook
Run all functions hooked
Continue WordPress process
WordPress runs its core, triggers action hooks at specific points, then runs all functions attached to those hooks before continuing.
Execution Sample
Wordpress
<?php
add_action('init', 'my_init_function');
function my_init_function() {
  error_log('Init hook fired');
}
?>
This code attaches a function to the 'init' action hook that logs a message when WordPress initializes.
Execution Table
StepAction Hook TriggeredHooked FunctionFunction OutputNext Step
1initmy_init_functionLogs 'Init hook fired' to error logContinue loading WordPress
2wp_headtheme_custom_headAdds custom CSS to page headerRender page head
3wp_footertheme_custom_footerAdds custom scripts before </body>Render page footer
4shutdowncleanup_functionPerforms cleanup tasksEnd WordPress process
💡 All hooked functions run at their action points; WordPress process continues normally.
Variable Tracker
VariableStartAfter initAfter wp_headAfter wp_footerAfter shutdown
error_logempty'Init hook fired''Init hook fired''Init hook fired''Init hook fired'
page_heademptyemptycustom CSS addedcustom CSS addedcustom CSS added
page_footeremptyemptyemptycustom scripts addedcustom scripts added
cleanup_donefalsefalsefalsefalsetrue
Key Moments - 2 Insights
Why does my hooked function not run if I add it after WordPress loads?
Hooks run only when WordPress triggers them during its process. Adding functions after the hook triggers means they won't run. See execution_table step 1 where 'init' runs early.
Can multiple functions run on the same action hook?
Yes, WordPress runs all functions hooked to an action in the order they were added. The execution_table shows different hooks but multiple functions can attach to one hook.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what output does 'my_init_function' produce at step 1?
AAdds custom scripts before </body>
BAdds custom CSS to page header
CLogs 'Init hook fired' to error log
DPerforms cleanup tasks
💡 Hint
Check the 'Function Output' column for step 1 in execution_table.
At which step does WordPress add custom scripts before the closing body tag?
Ainit
Bwp_footer
Cwp_head
Dshutdown
💡 Hint
Look at the 'Action Hook Triggered' and 'Function Output' columns in execution_table.
If you add a function to 'shutdown' hook, when will it run?
AAt the very end of WordPress process
BDuring page header rendering
CBefore WordPress loads core
DImmediately after 'init' hook
💡 Hint
Refer to the last row in execution_table and variable_tracker for 'cleanup_done'.
Concept Snapshot
WordPress action hooks let you run your code at specific points.
Use add_action('hook_name', 'your_function') to attach.
WordPress triggers hooks during loading, rendering, and shutdown.
All hooked functions run in order when the hook fires.
Hooks help customize behavior without changing core files.
Full Transcript
WordPress uses action hooks to let developers add custom code at key points. When WordPress loads, it triggers hooks like 'init', 'wp_head', 'wp_footer', and 'shutdown'. Functions attached to these hooks run automatically. For example, a function hooked to 'init' runs early to set up things. Multiple functions can attach to the same hook and run in order. Hooks run only when WordPress triggers them, so adding functions too late means they won't run. This system helps customize WordPress safely and flexibly.