WordPress的:如何获得'the_content'filter的所有注册function

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

有谁知道如何获得将被应用或“注册”到the_contentfilter的所有function的数组或列表?

这个想法是生成一个可选function的checkbox列表,从filter中删除,如wpautop。 我知道如何从硬编码标签的filter中删除函数,但我希望能够创build一个更dynamic的解决scheme。

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

简单的function从filterarrays打印?

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

把它叫做你需要它的地方。

 print_filters_for( 'the_content' ); 

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

为了得到一个挂钩在特定动作(或filter)上的函数的基本列表,只要从filter数组中获取项就足够了,但是由于函数可以以各种方式(作为类方法或闭包)附加,所以列表将包含大量的包含呈现为string的对象的相关数据。 这个例子将只显示相关的数据,按优先顺序排列:

  • 函数名称(取决于callback语法):
    • 函数callback: 'function_name'
    • 对象方法: array( $object, 'function_name' )
    • 静态类方法: array( 'class_name', 'function_name' )'class_name::function_name'
    • closure: 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动作是获取完整列表的好地方)

print_r示例为the_contentfilter:

 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
  • 优先考虑
  • 修正:如果callback不存在,则引发错误,尽pipeWordPress也提出了一个警告
  • 固定:具有相同ID但不同优先级的钩子覆盖前一个