WordPress:如何获取'the_content'过滤器的所有注册函数

mac*_*000 57 wordpress filter

我对WordPress有疑问,特别是3.0及更高版本.

有谁知道如何获得将应用或"注册"到the_content过滤器的所有函数的数组或列表?

我们的想法是生成一个可以从过滤器中删除的可能函数的复选框列表,例如wpautop.我知道如何使用硬编码标签从过滤器中删除功能,但我希望创建一个更动态的解决方案.

如果有人有任何想法,如果这是可能的以及如何做到这一点我会非常感兴趣.谢谢.

t31*_*1os 116

从过滤器阵列打印的简单功能?

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

    print '<pre>';
    print_r( $wp_filter[$hook] );
    print '</pre>';
}
Run Code Online (Sandbox Code Playgroud)

在需要的地方拨打电话.

print_filters_for( 'the_content' );
Run Code Online (Sandbox Code Playgroud)

  • 真棒.感谢您的回复,我不知道wp_filter全局,但现在一切都有道理.干杯:) (3认同)

Dan*_*jel 26

这是一个更高级的示例,除了来自$wp_filter数组的数据之外,还将显示附加挂钩的文件的路径,以及定义函数的代码行.

要获取挂钩在特定操作(或过滤器)上的函数的基本列表,从过滤器数组中获取项是足够的,但由于函数可以以各种方式(作为类方法或闭包)附加,该列表将包含大量的相关数据,包括以字符串形式呈现的对象.此示例将按优先级顺序仅显示相关数据:

  • 函数名称(取决于回调语法):
    • 函数回调: 'function_name'
    • 对象方法: array( $object, 'function_name' )
    • 静态类方法:array( 'class_name', 'function_name' )'class_name::function_name'
    • 关闭: function() {}
    • 相对静态类方法: array( 'class_name', 'parent::function_name' )
  • 接受了args
  • 文件名
  • 起跑线
  • ID
  • 优先

function list_hooks( $hook = '' ) {
    global $wp_filter;

    if ( isset( $wp_filter[$hook]->callbacks ) ) {      
        array_walk( $wp_filter[$hook]->callbacks, function( $callbacks, $priority ) use ( &$hooks ) {           
            foreach ( $callbacks as $id => $callback )
                $hooks[] = array_merge( [ 'id' => $id, 'priority' => $priority ], $callback );
        });         
    } else {
        return [];
    }

    foreach( $hooks as &$item ) {
        // skip if callback does not exist
        if ( !is_callable( $item['function'] ) ) continue;

        // function name as string or static class method eg. 'Foo::Bar'
        if ( is_string( $item['function'] ) ) {
            $ref = strpos( $item['function'], '::' ) ? new ReflectionClass( strstr( $item['function'], '::', true ) ) : new ReflectionFunction( $item['function'] );
            $item['file'] = $ref->getFileName();
            $item['line'] = get_class( $ref ) == 'ReflectionFunction' 
                ? $ref->getStartLine() 
                : $ref->getMethod( substr( $item['function'], strpos( $item['function'], '::' ) + 2 ) )->getStartLine();

        // array( object, method ), array( string object, method ), array( string object, string 'parent::method' )
        } elseif ( is_array( $item['function'] ) ) {

            $ref = new ReflectionClass( $item['function'][0] );

            // $item['function'][0] is a reference to existing object
            $item['function'] = array(
                is_object( $item['function'][0] ) ? get_class( $item['function'][0] ) : $item['function'][0],
                $item['function'][1]
            );
            $item['file'] = $ref->getFileName();
            $item['line'] = strpos( $item['function'][1], '::' )
                ? $ref->getParentClass()->getMethod( substr( $item['function'][1], strpos( $item['function'][1], '::' ) + 2 ) )->getStartLine()
                : $ref->getMethod( $item['function'][1] )->getStartLine();

        // closures
        } elseif ( is_callable( $item['function'] ) ) {     
            $ref = new ReflectionFunction( $item['function'] );         
            $item['function'] = get_class( $item['function'] );
            $item['file'] = $ref->getFileName();
            $item['line'] = $ref->getStartLine();

        }       
    }

    return $hooks;
}
Run Code Online (Sandbox Code Playgroud)

由于可以在整个运行时添加和删除钩子,因此输出取决于调用函数的位置(wp_footer操作是获取完整列表的好地方)

print_rthe_content过滤示例:

Array
(
    [0] => Array
        (
            [id] => 000000004c8a4a660000000011808a14run_shortcode
            [priority] => 8
            [function] => Array
                (
                    [0] => WP_Embed
                    [1] => run_shortcode
                )

            [accepted_args] => 1
            [file] => C:\xampp\htdocs\wordpress\wp-includes\class-wp-embed.php
            [line] => 58
        )

    [1] => Array
        (
            [id] => wptexturize
            [priority] => 10
            [function] => wptexturize
            [accepted_args] => 1
            [file] => C:\xampp\htdocs\wordpress\wp-includes\formatting.php
            [line] => 41
        )

    [2] => Array
        (
            [id] => 0000000006c5dc6d0000000064b1bc8e
            [priority] => 10
            [function] => Closure
            [accepted_args] => 1
            [file] => C:\xampp\htdocs\wordpress\wp-content\plugins\plugin\plugin.php
            [line] => 16
        )

    .....
Run Code Online (Sandbox Code Playgroud)

编辑:2017-05-05

  • 适合WP_Hook上课
  • 增加优先权
  • 修复:如果回调不存在则引发错误,尽管WordPress也会发出警告
  • fixed:具有相同id但不同优先级的hook会覆盖前一个

  • 令人敬畏的调试'工具' - 干得好. (4认同)
  • 由于`$ wp_filter`是[现在是一个类](https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/),这将不再起作用 (2认同)