有没有办法知道WordPress中某个页面上的操作添加了哪些功能?
我如何才能找出为操作分配了哪些功能?
有一个神奇的动作all
在每个操作或筛选器上调用。您可以挂接到此操作并记录所有相关函数。在里面$GLOBALS[\'wp_filter\']
你也可以检查它,你可以找到所有活动的钩子,例如实际使用的钩子。
我写了一个小插件T5 Debug Hook 查看的所有函数one 挂钩:
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Debug Hook
* Description: Adds a list of registered filters and action for a hook. Call a page with <code>?hook=NAME</code> to see it.
*/
add_action( \'shutdown\', \'t5_debug_hook\' );
function t5_debug_hook()
{
if ( ! isset ( $_GET[\'hook\'] ) or ! current_user_can( \'update_core\') )
{
return;
}
$f = $GLOBALS[\'wp_filter\'];
if ( ! isset ( $f[ $_GET[\'hook\'] ] ) )
{
print \'Nothing found for \' . esc_html( $_GET[\'hook\'] );
return;
}
print \'<pre>\' . esc_html( var_export( $f[ $_GET[\'hook\'] ], TRUE ) ) . \'</pre>\';
}
要获取页面上所有可用操作的列表,请尝试以下操作:<?php # -*- coding: utf-8 -*-
/*
Plugin Name: All Actions List
Description: Lists all actions run during one request.
Version: 1.0
Required: 3.1
Author: Thomas Scholz
Author URI: http://toscho.de
License: GPL
*/
! defined( \'ABSPATH\' ) and exit;
add_action( \'all\', \'aal_handler\', 99999, 99 );
function aal_handler()
{
static $list = array ();
// exclude certain actions:
$exclude = array ( \'gettext\', \'gettext_with_context\' );
$action = current_filter();
if ( ! in_array( $action, $exclude ) )
{
$list[] = $action;
}
// shutdown is the last action
if ( \'shutdown\' == $action )
{
print \'<pre>\' . implode( "\\n", $list ) . \'</pre>\';
}
}
WordPress保存全局钩子信息的当前状态$wp_filter
变量,您可以检查该变量(例如var_dump()
). 请注意,这是在运行时构建的,因此在代码中添加某些内容之前,它不知道向挂钩添加了什么。
有一些辅助代码在浮动,我的是R_Debug::list_hooks().
正如Rarst所指出的,当回调连接到操作/过滤器时,它们会添加到$wp_filter
全球的通过检查,可以查看哪些函数已添加到该挂钩(每次激发挂钩时可能会有所不同)。
查看我的答案this question. 或者GitHub gist 这就是它的结果。
或者,您可以使用this plug-in, 它在管理员用户的管理栏中添加了一个搜索,允许您搜索在该页面上调用的挂钩。选择挂钩将显示一个对话框,其中包含已挂接到该操作/过滤器上的函数: