46

In what order add_action hooks execute?

i.e.

init
wp_head
wp_footer
after_theme_setup 
etc...
???
???
???




EDIT:

I've also posted my solution.

81

"Data! Data! Data!" he cried impatiently. "I can't make bricks without clay."

-The Adventure of the Copper Beeches

So let's gather some real data from an install with no plugins and the TwentyTwelve theme activated with only a single Text widget.

For the home page the following do_action calls are made in the following order:

muplugins_loaded
registered_taxonomy
registered_taxonomy
registered_taxonomy
registered_taxonomy
registered_taxonomy
registered_post_type
registered_post_type
registered_post_type
registered_post_type
registered_post_type
plugins_loaded
sanitize_comment_cookies
setup_theme
unload_textdomain
load_textdomain
after_setup_theme
load_textdomain
load_textdomain
auth_cookie_malformed
auth_cookie_valid
set_current_user
init
registered_post_type
registered_post_type
registered_post_type
registered_post_type
registered_post_type
registered_taxonomy
registered_taxonomy
registered_taxonomy
registered_taxonomy
registered_taxonomy
widgets_init
register_sidebar
register_sidebar
register_sidebar
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_register_sidebar_widget
wp_loaded
parse_tax_query
parse_tax_query
posts_selection
template_redirect
admin_bar_init
add_admin_bar_menus
get_header
wp_head
wp_enqueue_scripts
wp_print_styles
wp_print_scripts
get_template_part_content
begin_fetch_post_thumbnail_html
end_fetch_post_thumbnail_html
get_template_part_content
get_template_part_content
get_template_part_content
get_template_part_content
get_template_part_content
get_template_part_content
get_template_part_content
get_template_part_content
get_template_part_content
begin_fetch_post_thumbnail_html
end_fetch_post_thumbnail_html
get_sidebar
dynamic_sidebar_before
dynamic_sidebar
dynamic_sidebar_after
get_footer
twentytwelve_credits
wp_footer
wp_print_footer_scripts
wp_before_admin_bar_render
wp_after_admin_bar_render
shutdown

If you want to check the order of actions and how many times each one is fired, then you can use for example:

add_action( 'shutdown', function(){
    print_r( $GLOBALS['wp_actions'] ); 
});

or this prettified version:

add_action( 'shutdown', function(){
    foreach( $GLOBALS['wp_actions'] as $action => $count )
        printf( '%s (%d) <br/>' . PHP_EOL, $action, $count );

});

to get the following list:

muplugins_loaded (1) 
registered_taxonomy (10) 
registered_post_type (10) 
plugins_loaded (1) 
sanitize_comment_cookies (1) 
setup_theme (1) 
unload_textdomain (1) 
load_textdomain (3) 
after_setup_theme (1) 
auth_cookie_malformed (1) 
auth_cookie_valid (1) 
set_current_user (1) 
init (1) 
widgets_init (1) 
register_sidebar (3) 
wp_register_sidebar_widget (12) 
wp_loaded (1) 
parse_request (1) 
send_headers (1) 
parse_tax_query (2) 
parse_query (1) 
pre_get_posts (1) 
posts_selection (1) 
wp (1) 
template_redirect (1) 
wp_default_scripts (1) 
wp_default_styles (1) 
admin_bar_init (1) 
add_admin_bar_menus (1) 
get_header (1) 
wp_head (1) 
wp_enqueue_scripts (1) 
wp_print_styles (1) 
wp_print_scripts (1) 
loop_start (1) 
the_post (10) 
get_template_part_content (10) 
begin_fetch_post_thumbnail_html (2) 
end_fetch_post_thumbnail_html (2) 
loop_end (1) 
get_sidebar (1) 
dynamic_sidebar_before (1) 
dynamic_sidebar (1) 
dynamic_sidebar_after (1) 
get_footer (1) 
twentytwelve_credits (1) 
wp_footer (1) 
wp_print_footer_scripts (1) 
admin_bar_menu (1) 
wp_before_admin_bar_render (1) 
wp_after_admin_bar_render (1) 
shutdown (1) 

PS: You should also check out the great Query Monitor plugin by John Blackbourn. (I'm not related to this plugin)

  • Very nice indeed! – jdm2112 Mar 19 '15 at 19:30
  • Thank you for mentioning Query Monitor. Seems to be a useful plugin on this case. – D.A.H Oct 16 '15 at 9:53
  • @kraftner thanks for the update, I always planned (but forgot about it) to link directly to the story itself as a proper source, obviously my Sherlock Holmes search foo wasn't great at that time ;-) – birgire Mar 1 '17 at 13:22
  • 1
    I liked the quote and wanted to see more context. And as I already had the link for myself why not just update here as well. :) – kraftner Mar 1 '17 at 14:22
  • 1
    Over 4 yo post and still helpful. Thanks a lot! – Sebastian Kaczmarek Feb 5 at 10:15
19

Here is the WordPress load chart

WordPress Load Chart

Source by @Rarst

  • 9
    Add at least the source, or even better: find a duplicate for this question. – fuxia Sep 29 '14 at 8:36
  • 2
    Actually I did not know where I got it from. I had this image saved on my PC. Otherwise I would have done that. – Robert hue Sep 29 '14 at 8:43
  • It's also published on Tom Mc Farlin homepage: The WordPress Page Lifecycle -> tommcfarlin.com/wordpress-page-lifecycle – D.A.H Oct 16 '15 at 10:05
2

Found Solution!

Thanks @birgire for nice answer. I will add to that, muplugins_loaded sometimes is not fired, so I will use plugins_loaded as the most first hook (but at that time, user-authorization is not done yet. If you want to check user's authorization, then init is the earliest for that)...

p.s. there exist excellent plugins:

1) Query Monitor - You can see everything what happens on the page-load, i.e. duration of each executed function and much more(view all screenshots on plugin page):

enter image description here

2) WP-DEBUG-BAR + WP-DEBUG-SLOW-ACTIONS:
a) debug hooks(actions) run list on your site.
b) See duration of each action (not function): enter image description here

1

No two requests are exactly the same. A quick and dirty (but very accurate) way to find out what's going on is to temporarily add a line to the start of the do_action function in wp-includes/plugin.php which logs the $tag, e.g.:

if (isset($some_get_or_post_trigger_var)) file_put_contents(ABSPATH . 'action.log', "$tag\n", FILE_APPEND);
1

The basic sequence can also be found on the official docs:

https://codex.wordpress.org/Plugin_API/Action_Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.