如何知道哪些功能与操作/筛选器挂钩?

时间:2011-05-16 作者:Javier Villanueva

有没有办法知道哪些函数连接到特定的钩子上?例如,如果我想知道哪些函数连接到wp_head

6 个回复
最合适的回答,由SO网友:fuxia 整理而成

查看全局变量$wp_filter. 查看我的plugin for a list of all comment filters 例如:

<?php
/*
Plugin Name: List Comment Filters
Description: List all comment filters on wp_footer
Version:     1.1
Author:      Fuxia Scholz
License:     GPL v2
*/

add_action( \'wp_footer\', \'list_comment_filters\' );

function list_comment_filters()
{
    global $wp_filter;

    $comment_filters = array ();
    $h1  = \'<h1>Current Comment Filters</h1>\';
    $out = \'\';
    $toc = \'<ul>\';

    foreach ( $wp_filter as $key => $val )
    {
        if ( FALSE !== strpos( $key, \'comment\' ) )
        {
            $comment_filters[$key][] = var_export( $val, TRUE );
        }
    }

    foreach ( $comment_filters as $name => $arr_vals )
    {
        $out .= "<h2 id=$name>$name</h2><pre>" . implode( "\\n\\n", $arr_vals ) . \'</pre>\';
        $toc .= "<li><a href=\'#$name\'>$name</a></li>";
    }

    print "$h1$toc</ul>$out";
}
的示例输出pre_comment_author_email:

array (
  10 => 
  array (
    \'trim\' => 
    array (
      \'function\' => \'trim\',
      \'accepted_args\' => 1,
    ),
    \'sanitize_email\' => 
    array (
      \'function\' => \'sanitize_email\',
      \'accepted_args\' => 1,
    ),
    \'wp_filter_kses\' => 
    array (
      \'function\' => \'wp_filter_kses\',
      \'accepted_args\' => 1,
    ),
  ),
)

SO网友:Aamer Shahzad

要查看连接到特定动作挂钩的函数或动作列表,可以使用以下代码。

global $wp_filter;
echo \'<pre>\';
var_dump( $wp_filter[\'wp_head\'] );
echo \'</pre>\';

SO网友:Martin from WP-Stars.com

出于调试目的

global $wp_filter;
echo "<pre>" . print_r($wp_filter, true) . "</pre>";
会这样做的。。。

SO网友:Simone G

这显示了一个更可读的过滤器列表

function print_filters_for( $hook = \'\' ) {
    global $wp_filter;
    if( empty( $hook ) || !isset( $wp_filter[$hook] ) ) return;

    $ret=\'\';
    foreach($wp_filter[$hook] as $priority => $realhook){
        foreach($realhook as $hook_k => $hook_v){
            $hook_echo=(is_array($hook_v[\'function\'])?get_class($hook_v[\'function\'][0]).\':\'.$hook_v[\'function\'][1]:$hook_v[\'function\']);
            $ret.=  "\\n$priority $hook_echo";
        }

    }
     return $ret;
}

SO网友:Derwent

我发现@Simone G的答案很有用,但它没有考虑到这样一个事实,即有时闭包可以被钩住。以下是我更冗长(丑陋)的版本:

if( isset($wp_filter[$filterName]) ){
    foreach( $wp_filter[$filterName] as $priority => $hooks){
        foreach ($hooks as $hook_k => $hook_v) {
            $hook_echo=(is_array($hook_v[\'function\'])?get_class($hook_v[\'function\'][0]).\':\'.$hook_v[\'function\'][1]:$hook_v[\'function\']);
            if(is_object($hook_echo) && ($hook_echo instanceof Closure)){
                $hook_echo="closure";
            }
            error_log($filterName." HOOKED (".serialize($priority)."): ".serialize($hook_k)."".serialize($hook_echo));
        }
    }
} else {
    error_log($filterName." NO FILTERS HOOKED");
}

SO网友:Rafiq

这节省了我的时间,因为我可以在任何页面上直接看到https://wordpress.org/plugins/show-hooks/

结束

相关推荐

Custom Post Row Actions

我偶然发现this question 在写这个问题的时候。我有一个问题是关于这个问题的。我发现你用的是get_delete_post_link 筛选为我的操作创建一个新的url(或一个类似的函数——在任何情况下,我都会将该函数与布尔值一起使用)。唯一的问题是,I don\'t know how to capture the event now. 考虑到我在谷歌上找不到很多关于行后操作的例子,我将不胜感激-/public function _wp_filter_get_delete_post_link( $