1

I'm working on a plugin that modifies user capabilities and turns off a lot of things in the Admin pages. I'm having some problems with order of execution with my code, so I want to refactor it and make sure I completely understand when action hooks fire in WP, so I can tie my code to those hooks correctly.

I did a simple trace of actions I'm interested in. I was a little surprised by the frequency of hooks firing. For example wp_loaded - you would think that happens once, but it must happen over and over again as the user traverses the admin pages. I don't understand that.

Here's my trace, I am hoping someone can explain why it looks this way (questions embedded):

----> At login dialog; enter username and password, click "log in" 

muplugins_loaded
plugins_loaded
set_current_user
init
wp_loaded
login_init
wp_login                (completed login)

muplugins_loaded        (ok, we are doing this again?)
plugins_loaded
set_current_user
init
wp_loaded

admin_menu
custom_menu_order
admin_init
admin_bar_init
admin_enqueue_scripts
admin_head
adminmenu
in_admin_header
admin_bar_menu
wp_before_admin_bar_render
wp_after_admin_bar_render
contextual_help
in_admin_footer
admin_footer_text
admin_footer
admin_print_footer_scripts      (completed admin page render?)

muplugins_loaded                (now all over again? - three times, why?)
plugins_loaded
set_current_user
init
wp_loaded

admin_menu
custom_menu_order
admin_init
admin_bar_init
admin_enqueue_scripts
admin_head
adminmenu
in_admin_header
admin_bar_menu
wp_before_admin_bar_render
wp_after_admin_bar_render
contextual_help
in_admin_footer
admin_footer_text
admin_footer
admin_print_footer_scripts

----> Viewing Admin dashboard 
1

I'm not sure if you mean some custom login dialog, but let's do some logging when we are on the wp-login.php page, where we fill the login form and press enter:

#-----------------    
# Page Load #1
#-----------------    
2015-11-24: 14:45:41 --- $_SERVER: Array
(
    [REQUEST_URI] => /wp-login.php
    [SCRIPT_NAME] => /wp-login.php
    [DOCUMENT_URI] => /wp-login.php
    [REDIRECT_STATUS] => 200
    [SCRIPT_FILENAME] => /example.tld/wp-login.php
    [HTTP_ORIGIN] => http://example.tld
    [HTTP_REFERER] => http://example.tld/wp-login.php
    [PHP_SELF] => /wp-login.php
)

2015-11-24: 14:45:41 --- $_GET: Array
(
)

2015-11-24: 14:45:41 --- $_POST: Array
(
    [log] => user
    [pwd] => pass
    [wp-submit] => Log In
    [redirect_to] => http://exmple.tld/wp-admin/
    [testcookie] => 1
)

2015-11-24: 14:45:41 --- do_action: muplugins_loaded
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: plugins_loaded
2015-11-24: 14:45:41 --- do_action: sanitize_comment_cookies
2015-11-24: 14:45:41 --- do_action: setup_theme
2015-11-24: 14:45:41 --- do_action: unload_textdomain
2015-11-24: 14:45:41 --- do_action: load_textdomain
2015-11-24: 14:45:41 --- do_action: after_setup_theme
2015-11-24: 14:45:41 --- do_action: load_textdomain
2015-11-24: 14:45:41 --- do_action: load_textdomain
2015-11-24: 14:45:41 --- do_action: auth_cookie_malformed
2015-11-24: 14:45:41 --- do_action: set_current_user
2015-11-24: 14:45:41 --- do_action: init
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: widgets_init
2015-11-24: 14:45:41 --- do_action: register_sidebar
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: update_user_meta
2015-11-24: 14:45:41 --- do_action: updated_user_meta
2015-11-24: 14:45:41 --- do_action: update_user_meta
2015-11-24: 14:45:41 --- do_action: remove_user_role
2015-11-24: 14:45:41 --- do_action: update_user_meta
2015-11-24: 14:45:41 --- do_action: updated_user_meta
2015-11-24: 14:45:41 --- do_action: update_user_meta
2015-11-24: 14:45:41 --- do_action: add_user_role
2015-11-24: 14:45:41 --- do_action_ref_array: wp_default_scripts
2015-11-24: 14:45:41 --- do_action_ref_array: wp_default_styles
2015-11-24: 14:45:41 --- do_action: wp_loaded
2015-11-24: 14:45:41 --- do_action: login_init
2015-11-24: 14:45:41 --- do_action: login_form_login
2015-11-24: 14:45:41 --- do_action_ref_array: wp_authenticate
2015-11-24: 14:45:41 --- do_action: add_user_meta
2015-11-24: 14:45:41 --- do_action: added_user_meta
2015-11-24: 14:45:41 --- do_action: set_auth_cookie
2015-11-24: 14:45:41 --- do_action: set_logged_in_cookie
2015-11-24: 14:45:41 --- do_action: wp_login
2015-11-24: 14:45:41 --- ** Redirect location: http://example.tld/wp-admin/
2015-11-24: 14:45:41 --- ** Redirect status: 302
2015-11-24: 14:45:41 --- do_action: shutdown

#-----------------    
# Page Load #2
#-----------------    
2015-11-24: 14:45:41 --- $_SERVER: Array
(
    [REQUEST_URI] => /wp-admin/
    [QUERY_STRING] => 
    [REQUEST_METHOD] => GET
    [SCRIPT_NAME] => /wp-admin/index.php
    [DOCUMENT_URI] => /wp-admin/index.php
    [REDIRECT_STATUS] => 200
    [SCRIPT_FILENAME] => /example.tld//wp-admin/index.php
    [HTTP_REFERER] => http://example.tld/wp-login.php
    [PHP_SELF] => /wp-admin/index.php
)

2015-11-24: 14:45:41 --- $_GET: Array
(
)

2015-11-24: 14:45:41 --- $_POST: Array
(
)

2015-11-24: 14:45:41 --- do_action: muplugins_loaded
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: plugins_loaded
2015-11-24: 14:45:41 --- do_action: sanitize_comment_cookies
2015-11-24: 14:45:41 --- do_action: setup_theme
2015-11-24: 14:45:41 --- do_action: unload_textdomain
2015-11-24: 14:45:41 --- do_action: load_textdomain
2015-11-24: 14:45:41 --- do_action: load_textdomain
2015-11-24: 14:45:41 --- do_action: after_setup_theme
2015-11-24: 14:45:41 --- do_action: load_textdomain
2015-11-24: 14:45:41 --- do_action: load_textdomain
2015-11-24: 14:45:41 --- do_action: auth_cookie_valid
2015-11-24: 14:45:41 --- do_action: set_current_user
2015-11-24: 14:45:41 --- do_action: init
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_taxonomy
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: widgets_init
2015-11-24: 14:45:41 --- do_action: register_sidebar
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: wp_register_sidebar_widget
2015-11-24: 14:45:41 --- do_action: registered_post_type
2015-11-24: 14:45:41 --- do_action: update_user_meta
2015-11-24: 14:45:41 --- do_action: updated_user_meta
2015-11-24: 14:45:41 --- do_action: update_user_meta
2015-11-24: 14:45:41 --- do_action: remove_user_role
2015-11-24: 14:45:41 --- do_action: update_user_meta
2015-11-24: 14:45:41 --- do_action: updated_user_meta
2015-11-24: 14:45:41 --- do_action: update_user_meta
2015-11-24: 14:45:41 --- do_action: add_user_role
2015-11-24: 14:45:41 --- do_action_ref_array: wp_default_scripts
2015-11-24: 14:45:41 --- do_action_ref_array: wp_default_styles
2015-11-24: 14:45:41 --- do_action: load_textdomain
2015-11-24: 14:45:41 --- do_action: load_textdomain
2015-11-24: 14:45:41 --- do_action: wp_loaded
2015-11-24: 14:45:41 --- do_action: auth_cookie_valid
2015-11-24: 14:45:41 --- do_action: auth_redirect
2015-11-24: 14:45:41 --- do_action: _admin_menu
2015-11-24: 14:45:41 --- do_action: admin_menu
2015-11-24: 14:45:41 --- do_action: admin_init
2015-11-24: 14:45:41 --- do_action: admin_bar_init
2015-11-24: 14:45:41 --- do_action: add_admin_bar_menus
2015-11-24: 14:45:41 --- do_action: test_custom_rewrite
2015-11-24: 14:45:41 --- do_action: current_screen
2015-11-24: 14:45:41 --- do_action: load-index.php
2015-11-24: 14:45:41 --- do_action: wp_dashboard_setup
2015-11-24: 14:45:41 --- do_action: do_meta_boxes
2015-11-24: 14:45:41 --- do_action: do_meta_boxes
2015-11-24: 14:45:41 --- do_action: admin_xml_ns
2015-11-24: 14:45:41 --- do_action: admin_xml_ns
2015-11-24: 14:45:41 --- do_action: admin_enqueue_scripts
2015-11-24: 14:45:41 --- do_action: admin_print_styles-index.php
2015-11-24: 14:45:41 --- do_action: admin_print_styles
2015-11-24: 14:45:41 --- do_action: admin_print_scripts-index.php
2015-11-24: 14:45:41 --- do_action: admin_print_scripts
2015-11-24: 14:45:41 --- do_action: wp_print_scripts
2015-11-24: 14:45:41 --- do_action: admin_head-index.php
2015-11-24: 14:45:41 --- do_action: admin_head
2015-11-24: 14:45:41 --- do_action: adminmenu
2015-11-24: 14:45:41 --- do_action: in_admin_header
2015-11-24: 14:45:41 --- do_action_ref_array: admin_bar_menu
2015-11-24: 14:45:41 --- do_action: wp_before_admin_bar_render
2015-11-24: 14:45:41 --- do_action: wp_after_admin_bar_render
2015-11-24: 14:45:41 --- do_action: admin_notices
2015-11-24: 14:45:41 --- do_action: all_admin_notices
2015-11-24: 14:45:41 --- do_action: welcome_panel
2015-11-24: 14:45:41 --- do_action: rightnow_end
2015-11-24: 14:45:41 --- do_action: activity_box_end
2015-11-24: 14:45:41 --- do_action: parse_tax_query
2015-11-24: 14:45:41 --- do_action_ref_array: parse_query
2015-11-24: 14:45:41 --- do_action_ref_array: pre_get_posts
2015-11-24: 14:45:41 --- do_action: parse_tax_query
2015-11-24: 14:45:41 --- do_action: posts_selection
2015-11-24: 14:45:41 --- do_action: parse_tax_query
2015-11-24: 14:45:41 --- do_action_ref_array: parse_query
2015-11-24: 14:45:41 --- do_action_ref_array: pre_get_posts
2015-11-24: 14:45:41 --- do_action: parse_tax_query
2015-11-24: 14:45:41 --- do_action: posts_selection
2015-11-24: 14:45:41 --- do_action_ref_array: loop_start
2015-11-24: 14:45:41 --- do_action_ref_array: the_post
2015-11-24: 14:45:41 --- do_action_ref_array: the_post
2015-11-24: 14:45:41 --- do_action_ref_array: the_post
2015-11-24: 14:45:41 --- do_action_ref_array: the_post
2015-11-24: 14:45:41 --- do_action_ref_array: the_post
2015-11-24: 14:45:41 --- do_action_ref_array: loop_end
2015-11-24: 14:45:41 --- do_action_ref_array: parse_comment_query
2015-11-24: 14:45:41 --- do_action_ref_array: pre_get_comments
2015-11-24: 14:45:42 --- do_action: parse_tax_query
2015-11-24: 14:45:42 --- do_action_ref_array: parse_query
2015-11-24: 14:45:42 --- do_action_ref_array: pre_get_posts
2015-11-24: 14:45:42 --- do_action: parse_tax_query
2015-11-24: 14:45:42 --- do_action: posts_selection
2015-11-24: 14:45:42 --- do_action: in_admin_footer
2015-11-24: 14:45:42 --- do_action: admin_footer
2015-11-24: 14:45:42 --- do_action: admin_print_footer_scripts
2015-11-24: 14:45:42 --- do_action: wp_enqueue_editor
2015-11-24: 14:45:42 --- do_action: before_wp_tiny_mce
2015-11-24: 14:45:42 --- do_action: wp_tiny_mce_init
2015-11-24: 14:45:42 --- do_action: after_wp_tiny_mce
2015-11-24: 14:45:42 --- do_action: admin_footer-index.php
2015-11-24: 14:45:42 --- do_action: shutdown

We see that we are redirected from wp-login.php to /wp-admin/index.php - no surprise there ;-)

So a redirection (or ajax calls?) might explain some duplicate part of your logging.

Update: I marked the redirect call with **.

  • makes sense, thank you - so any redirection or reloading pages will run the entire set of actions again. I'm still a little surprised that things like "mu_plugins" will fire multiple times...but there must be a good reason... – C C Nov 24 '15 at 20:08
0

The order of the hooks should not be important. If your plugin relays on the order then maybe you are doing something that is not very stable.

Hooks are fired at extension point, giving you ability to extend the functionality at that specific point. How you arrived there should not matter.

Some hooks like *_head *_init and *_footer have an obvious sequence of events assumption embedded in them, but almost anything other should be execution order independent.

You might think that you know the execution order after you have done the trace, but the order might be different if you come from XML-RPC or REST API or uploading an image or handling ajax.

(and the direct answer to your question is that your code that is doing the trace probably has bugs)

  • Mark, the code doing the trace is very simple: if (in_array(current_filter(),$yes_arr)) error_log(current_filter()); where the 'yes_arr' is an array of the hooks I want to monitor. This is loaded in mu_plugins. Fires on action 'all'. Regarding your point about order of the hooks, here's an example: I want to assure that any logged-in user who has role "subscriber" is assigned a specific set of capabilities. That has to happen before the admin menu is built. If I can't find a hook that is guaranteed to fire before admin_menu then you are correct my entire design is wrong. – C C Nov 24 '15 at 20:05
  • @CC init runs once per wordpress bootstrap, so if you see more then one init it means that you are logging more then one request. As for the other issue, just ask a specific question please. – Mark Kaplun Nov 25 '15 at 3:59
0

The frequency and order of hooks can vary based on each theme or plugin you you have installed/activated, but obviously there is a typical order for core actions and filters. do_action( 'muplugins_loaded' ) is in wp-settings.php so you'll see that event each time PHP includes that file. Any request to WordPress will usually make sure it is loaded.

If you want to know how often and which hooks run on your site then I highly recommend installing the plugin Query Monitor for WordPress.

Debug Bar is another plugin that might help you understand what's going on.

And if you want to see ALL the filters, then you can always add this to your functions.php

if ( !function_exists( 'debug_all_callback')) {

function debug_all_callback($arg1 = '')
{
    global $filters_called;
    $filters_called[] = $arg1;
}

function debug_on_shutdown_callback()
{
    global $filters_called;
    echo "<pre>";
    print_r($filters_called);
    echo "</pre>";
}

// event collector
global $filters_called;
$filters_called = array();

// subscribe to all hooks and filters
add_filter( "all", "debug_all_callback");
add_action( 'all', "debug_all_callback" );

// output list to screen
add_action("shutdown", "debug_on_shutdown_callback");

}

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.