我在哪里可以找到所有WordPress挂钩和over ridable函数(可插入、可编写脚本等)的列表?
Edit: 插件是listed here.
我在哪里可以找到所有WordPress挂钩和over ridable函数(可插入、可编写脚本等)的列表?
Edit: 插件是listed here.
@阿伦:正如基思S所指出的Adam Brown\'s List of Hooks 是WordPress的hooks的实际资源。然而,这并不完美:
它不按调用钩子的时间顺序显示钩子,
我一直在考虑这个想法,所以你的问题促使我写了一篇plugin 已调用“Instrument Hooks for WordPress.“你可以找到full source below 屏幕截图,您也可以download it from gist here.
下面是仪器的屏幕截图:
您可以使用URL参数触发检测instrument=hooks
, 即。:
http://example.com?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;
}
}
}
法典有一个Action Reference 和aFilter Reference. Adam Brown创建了hook database 它在源代码中包含所有挂钩,并添加来自wiki页面的文档、版本信息和到源代码的链接。您可以通过在Codex中编写文档来改进它。
当然,有些挂钩是动态的,这取决于其他数据。拿这个wp_transition_post_status
功能:
function wp_transition_post_status($new_status, $old_status, $post) {
do_action(\'transition_post_status\', $new_status, $old_status, $post);
do_action("${old_status}_to_$new_status", $post);
do_action("${new_status}_$post->post_type", $post->ID, $post);
}
如果注册自定义帖子类型event
和自定义帖子状态cancelled
, 您将有一个cancelled_event
行动挂钩。虽然很原始,但这个插件代码可能会有所帮助?如果您想查看过滤器,请将“添加操作”切换为“添加过滤器”。加载插件,然后刷新网站主页。一旦加载,停用是一件非常痛苦的事情,所以只需重命名plugins文件夹下的插件文件,然后再次刷新站点,它就会自动停用。我已经多次使用这个技巧来解决问题或找到一个可以插入内容的地方。
<?php
/*
Plugin Name: Hooks
Plugin URI: http://example.com/
Description: Hooks
Version: 1.00
Author: Hooks
Author URI: http://example.com/
*/
add_action(\'all\',\'hook_catchall\');
function hook_catchall(&$s1 = \'\', &$s2 = \'\', &$s3 = \'\', &$s4 = \'\') {
echo "<h1>1</h1>\\n";
print_r($s1);
echo "<br />\\n";
echo "<h1>2</h1>\\n";
print_r($s2);
echo "<br />\\n";
echo "<h1>3</h1>\\n";
print_r($s3);
echo "<br />\\n";
echo "<h1>4</h1>\\n";
print_r($s4);
echo "<br />\\n";
return $s1;
}
我用这个来找到钩子的顺序。获取filters
换衣服add_action
到add_filter
.
function echo_all_hooks() {
$not_arr = array(\'gettext\',\'sanitize_key\',\'gettext_with_context\',\'attribute_escape\');
if(!in_array(current_filter(),$not_arr)) echo current_filter()."<br/>";
}
add_action(\'all\',\'echo_all_hooks\');
您可以只使用查询监视器插件:https://wordpress.org/plugins/query-monitor/