在写WordPress代码时需要不停地与hooks(actions and filters)打交道,filter是过滤器,顺序问题不是那么重要,而action是一种行为动作,明确其执行顺序就显得尤为重要。通过了解actions的执行顺序,可以知晓在这个action执行时,是否已经具备了某些资源,例如登陆用户信息、插件API等。下面列出了一些相对来说比较重要的actions执行顺序,以供参考:
muplugins_loaded(最先加载的action)
registered_taxonomy
registered_post_type
plugins_loaded(加载所有激活的插件文件,这是插件代码被执行的位置)
sanitize_comment_cookies
setup_theme(载入当前主题的functions.php,functions.php中没有用add_filter或add_action添加的函数在这里被执行)
after_setup_theme
auth_cookie_malformed
auth_cookie_valid
set_current_user(执行了wp_set_current_user()函数,产生全局变量$current_user)
init
widgets_init
register_sidebar
wp_register_sidebar_widget
wp_default_scripts
wp_default_styles
admin_bar_init
add_admin_bar_menus
wp_loaded
parse_request
send_headers
parse_query
pre_get_posts
posts_selection
wp
template_redirect(加载激活的主题模板,例如index.php、page.php等)
get_header
wp_head
wp_enqueue_scripts
wp_print_styles
wp_print_scripts
wp_print_scripts
get_footer
wp_footer
shutdown