WordPress:如何获取“the_content”筛选器的所有已注册函数?

6 浏览
0 Comments

WordPress:如何获取“the_content”筛选器的所有已注册函数?

我有一个关于WordPress的问题,具体是版本3.0及更新版本。

有人知道如何获得将应用或已“注册”到the_content过滤器的所有函数的数组或列表吗?

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

如果有人有任何想法,这是否可能及如何完成这一点,我会非常感兴趣。谢谢。

admin 更改状态以发布 2023年5月23日
0
0 Comments

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

获取特定操作(或者过滤器)上挂钩的基本函数列表,仅需要从过滤器数组中提取项目即可。但是由于函数可以以不同的方式附加(作为类方法或闭包),该列表将包含一堆不相关的数据,包括以字符串表示的对象。此例只会按优先级显示相关数据:

  • 函数名(根据回调语法):
    • 函数回调:'function_name'
    • 对象方法:array( $object, 'function_name' )
    • 静态类方法:array( 'class_name', 'function_name' )'class_name::function_name'
    • 闭包:function() {}
    • 相对静态类方法:array( 'class_name', 'parent::function_name' )
  • 接受的参数
  • 文件名
  • 起始行
  • 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;
}

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

对于 the_content 过滤器的print_r 示例:

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
        )
    .....


编辑:2017-05-05

  • 适配 WP_Hook
  • 添加优先级
  • 修复:回调不存在时引发错误,尽管 WordPress 也会为此发出警告
  • 修复:具有相同 id 但不同优先级的挂钩会覆盖之前的挂钩
0
0 Comments

一个简单的功能,用于从筛选器数组中打印?

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

'; }

在您需要它的地方调用它。

print_filters_for( 'the_content' );

0