DOOFOX BLOG

8 月 10 2017 Category:

WrodPress 页面加载 Hooks 执行顺序

对于想拦截某些页面请求或判断页面执行的Hook、了解 WordPress 的执行过程,你可以通过下列代码得到。

// 输出当前页面执行的hook和顺序
add_action( 'shutdown', function(){
    print_r( $GLOBALS['wp_actions'] ); 
});

你会得到类似这样的内容

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

优化一下显示方式,以便更快的查看执行次数

add_action( 'shutdown', function(){
    foreach( $GLOBALS['wp_actions'] as $action => $count )
        printf( '%s (%d) 
' . PHP_EOL, $action, $count ); });
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) 
浙ICP备17013116号-1