WordPress 显示 filter 和 action 钩子上的所有函数

WordPress 主题开发或者插件开发时,有时需要列出页面 filter 和 action 钩子上的所有函数,以便调试。主题开发中,将以下代码添加到 functions.php 末尾,即可在访问页面的时候列出 WordPress 钩子上的所有函数:

global $wp_filter;
echo "<pre>" . print_r($wp_filter, true) . "</pre>";

上面的代码会显示附加到 WordPress 所有 filter 和 action 钩子上的函数,而如果想要显示某个 hook 钩子上的函数,则将以下代码添加到 functions.php 末尾:

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

通过指定 hook 的名字即可在需要的地方进行调用显示:

print_filters_for( 'the_content' );

对于在页面显示出来的所有函数,如果想要确切知道附加到的 hook 到底是 action 还是 filter 可以通过 这个工具 进行查询。