wordpress中do_action的执行顺序

在4.0中,Action的执行顺序和优先级设置如下:(via)

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) 

但是这个顺序随着版本变化,有可能产生变化.所以有人做了一个插件.这里下载
安装后访问.via

http://yourblog/?instrument=hooks

即可查看所有的action和filter的加载情况.

下面是插件源代码

<?php
/*
Plugin Name: Instrument Hooks for WordPress
Description: Instruments Hooks for a Page. Outputs during the Shutdown Hook.
Version: 0.1
Author: Mike Schinkel
Author URI: http://mikeschinkel.com
*/

if ($_GET['instrument']=='hooks') {

    add_action('shutdown','instrument_hooks');
    function instrument_hooks() {
        global $wpdb;
        $hooks = $wpdb->get_results("SELECT * FROM wp_hook_list ORDER BY first_call");
        $html = array();
        $html[] = '<style>#instrumented-hook-list table,#instrumented-hook-list th,#instrumented-hook-list td {border:1px solid gray;padding:2px 5px;}</style>
<div align="center" id="instrumented-hook-list">
    <table>
        <tr>
        <th>First Call</th>
        <th>Hook Name</th>
        <th>Hook Type</th>
        <th>Arg Count</th>
        <th>Called By</th>
        <th>Line #</th>
        <th>File Name</th>
        </tr>';
        foreach($hooks as $hook) {
            $html[] = "<tr>
            <td>{$hook->first_call}</td>
            <td>{$hook->hook_name}</td>
            <td>{$hook->hook_type}</td>
            <td>{$hook->arg_count}</td>
            <td>{$hook->called_by}</td>
            <td>{$hook->line_num}</td>
            <td>{$hook->file_name}</td>
            </tr>";
        }
        $html[] = '</table></div>';
        echo implode("\n",$html);
    }

    add_action('all','record_hook_usage');
    function record_hook_usage($hook){
        global $wpdb;
        static $in_hook = false;
        static $first_call = 1;
        static $doc_root;
        $callstack = debug_backtrace();
        if (!$in_hook) {
            $in_hook = true;
            if ($first_call==1) {
                $doc_root = $_SERVER['DOCUMENT_ROOT'];
                $results = $wpdb->get_results("SHOW TABLE STATUS LIKE 'wp_hook_list'");
                if (count($results)==1) {
                    $wpdb->query("TRUNCATE TABLE wp_hook_list");
                } else {
                    $wpdb->query("CREATE TABLE wp_hook_list (
                    called_by varchar(96) NOT NULL,
                    hook_name varchar(96) NOT NULL,
                    hook_type varchar(15) NOT NULL,
                    first_call int(11) NOT NULL,
                    arg_count tinyint(4) NOT NULL,
                    file_name varchar(128) NOT NULL,
                    line_num smallint NOT NULL,
                    PRIMARY KEY (first_call,hook_name))"
                    );
                }
            }
            $args = func_get_args();
            $arg_count = count($args)-1;
            $hook_type = str_replace('do_','',
                str_replace('apply_filters','filter',
                    str_replace('_ref_array','[]',
                        $callstack[3]['function'])));
            $file_name = str_replace($doc_root,'',$callstack[3]['file']);
            $line_num = $callstack[3]['line'];
            $called_by = $callstack[4]['function'];
            $wpdb->query("INSERT wp_hook_list
                (first_call,called_by,hook_name,hook_type,arg_count,file_name,line_num)
                VALUES ($first_call,'$called_by()','$hook','$hook_type',$arg_count,'$file_name',$line_num)");
            $first_call++;
            $in_hook = false;
        }
    }
}

已有5条评论

  1. 已经抛弃WordPress~

    1. 确实它现在越来越臃肿了.但对于一个从1.50开始用的老家伙来说,根本放不下.

      1. 搜到了就给自己留个备份.

  2. 图片里男主角的衣服,我上个暑假也买过一件,巴顿图案。

    1. 好犀利的眼神

      1. 他指的是乔治巴顿吗?

        1. 不认识.那件T恤图案太小了,只能说他眼神太好了

  3. 还不会hook,相信快了。。呵呵

    1. 挺简单的.有编程基础的很容易写自己的hook

你好,新朋友。留言前请先填写昵称邮箱