問題描述
在哪裏可以找到所有 WordPress 鈎子和 over-ridable 功能 (可插拔,可腳本化等) 的列表?
編輯:插件是 listed here 。
最佳解決方案
@Arlen:正如 Keith S 指出的那樣,Adam Brown’s List of Hooks 是 WordPress 的鈎子的 defacto 資源。但是,這並不完美:
- 當它們被調用時,它不按順序顯示掛鈎,
- 它不提供調用的文件名或行號,
- 它沒有提供一些傳遞的參數,
- 它不是一個完整的列表,因為一些鈎子可以動態調用,
- 而且它不會顯示插件的鈎子。
所以當亞當的列表是一個偉大的資源,特別是為了瞭解鈎子在歷史上的添加,它幾乎沒有那麼有用,如果你能夠在你自己的網站上的任何一個頁面上掛鈎。
我一直在玩這個想法一段時間,所以你的問題引發我編寫一個插件,稱為 「樂器鈎子為 WordPress」 。您可以在屏幕截圖下方找到完整的源代碼,也可以從 GIST here 下載。
所以這裏是一個屏幕截圖的儀器看起來像:
您可以使用網址參數 instrument=hooks 觸發儀器,即:
和承諾,這裏的來源 (或下載它 here 。):
<?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;
}
}
}
次佳解決方案
Displays a list of actions fired for the current request. Requires the debug bar plugin.
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
