试图了解动作钩发射的顺序和频率

时间:2015-11-24 作者:C C

我正在开发一个插件,它可以修改用户功能并关闭管理页面中的许多内容。我对代码的执行顺序有一些问题,所以我想对其进行重构,并确保我完全理解WP中何时触发动作挂钩,以便我可以将代码正确地绑定到这些挂钩上。

我做了一个简单的跟踪我感兴趣的动作。我对frequency 钩子的射击。例如wp_loaded - 您可能认为这种情况只发生过一次,但当用户遍历管理页面时,这种情况必须反复发生。我不明白。

这是我的追踪,我希望有人能解释为什么它看起来是这样(嵌入了问题):

----> 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 

3 个回复
最合适的回答,由SO网友:birgire 整理而成

我不确定您是指一些自定义登录对话框,但让我们在wp-login.php 第页,填写登录表单,然后按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
我们看到我们是redirected 从…起wp-login.php/wp-admin/index.php - 毫不奇怪;-)

那么重定向(或者ajax调用?)可能解释了日志记录的某些重复部分

Update: 我将重定向呼叫标记为**.

SO网友:Mark Kaplun

挂钩的顺序应该不重要。如果你的插件按顺序运行,那么你可能正在做一些不太稳定的事情。

钩子在扩展点激发,使您能够在该特定点扩展功能。你是如何到达那里的并不重要。

有些钩子,如*\\u head*\\u init和*\\u footer,其中嵌入了一个明显的事件序列假设,但几乎所有其他钩子都应该是独立于执行顺序的。

您可能认为在完成跟踪之后就知道了执行顺序,但如果您来自XML-RPC或REST API,或者上载图像或处理ajax,则执行顺序可能会有所不同。

(对您的问题的直接回答是,进行跟踪的代码可能有bug)

SO网友:jgraup

钩子的频率和顺序可以根据您安装/激活的每个主题或插件而有所不同,但很明显,核心操作和过滤器有一个典型的顺序。do\\u操作(“muplugins\\u loaded”)在wp设置中。所以每次php包含该文件时,您都会看到该事件。对WordPress的任何请求通常都会确保已加载。

如果你想知道在你的网站上运行的频率和钩子,那么我强烈建议你安装这个插件Query Monitor 用于WordPress。

Debug Bar 是另一个可以帮助您了解情况的插件。

如果您想查看所有过滤器,那么您可以随时将其添加到函数中。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");

}

相关推荐

About Hooks and Filters

嗯,我很难理解动作和过滤器之间的区别。我确实在代码中使用动作,但我是一个新手,甚至连一点过滤器都不知道。我去过codex,以及NickTheGeek、BillErickson、GaryJones等的多个网站,但没有去过vein。如果你能用简单的话告诉我,并举例说明动作、过滤器和挂钩的基本内容和区别。非常感谢。