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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5224209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:44:44  来源:igfitidea点击:

WordPress: How do I get all the registered functions for 'the_content' filter

wordpressfilter

提问by macguru2000

I have a question regarding WordPress, specifically version 3.0 and newer.

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

Does anyone know how to get an array or list of all the functions that will be applied or are 'registered' to the_content filter?

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

The idea is to generate a checkbox list of possible functions to remove from the filter, such as wpautop. I know how to remove functions from the filter with hard coded labels, but I am hoping to create a more dynamic solution.

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

If anyone has any ideas if this is possible and how it could be done I would be very interested. Thanks.

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

回答by t31os

Simple function to print from the filter array?

从过滤器数组打印的简单函数?

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>';
}

Call it where you need it.

在需要的地方调用它。

print_filters_for( 'the_content' );

回答by Danijel

This is a bit more advanced example, that will, in addition to data from $wp_filterarray, show the path of the file where the hook is attached, as well as the line in code where the function is defined.

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

To get a basic list of functions hooked on a specific action ( or filter ) it is enough to fetch the items from the filter array, but since the functions can be attached in various ways ( as a class method or closure ) that list will contain a ton of irelevant data that includes objects presented as string. This example will display only the relevant data, in order of priority:

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

  • function name ( depending on callbacks syntax ):
    • function callback: 'function_name'
    • object method: array( $object, 'function_name' )
    • static class method: array( 'class_name', 'function_name' )and 'class_name::function_name'
    • closure: function() {}
    • relative static class method: array( 'class_name', 'parent::function_name' )
  • accepted args
  • file name
  • start line
  • id
  • priority
  • 函数名称(取决于回调语法):
    • 函数回调: '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;
}

Since hooks can be added and removed throughout the entire runtime, the output depends on at what point the function is called ( wp_footeraction is a good place to get the complete list )

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

print_rexample for the_contentfilter:

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
        )

    .....


Edit: 2017-05-05

编辑:2017-05-05

  • adapted for WP_Hookclass
  • added priority
  • fixed: error raised if callback does not exists, although WordPress also raises a warning for that
  • fixed: hook with the same id but different priority overwrites the previous one
  • 适应WP_Hook课堂
  • 增加优先权
  • 修复:如果回调不存在,则会引发错误,尽管 WordPress 也会为此发出警告
  • 固定:具有相同id但不同优先级的钩子覆盖前一个