問題描述
有沒有辦法知道什麼功能掛鈎到一個特定的鈎子?例如,如果我想知道什麼功能掛鈎到 wp_head 鈎子。
最佳解決方案
查看全局變量 $wp_filter 。以我的 plugin for a list of all comment filters 為例:
<?php
/*
Plugin Name: List Comment Filters
Description: List all comment filters on wp_footer
Version: 1.1
Author: Thomas Scholz
Author URI: http://toscho.de
License: GPL v2
*/
add_action( 'wp_footer', 'list_comment_filters' );
function list_comment_filters()
{
global $wp_filter;
$comment_filters = array ();
$h1 = '<h1>Current Comment Filters</h1>';
$out = '';
$toc = '<ul>';
foreach ( $wp_filter as $key => $val )
{
if ( FALSE !== strpos( $key, 'comment' ) )
{
$comment_filters[$key][] = var_export( $val, TRUE );
}
}
foreach ( $comment_filters as $name => $arr_vals )
{
$out .= "<h2 id=$name>$name</h2><pre>" . implode( "nn", $arr_vals ) . '</pre>';
$toc .= "<li><a href='#$name'>$name</a></li>";
}
print "$h1$toc</ul>$out";
}
pre_comment_author_email 的樣品輸出:
array (
10 =>
array (
'trim' =>
array (
'function' => 'trim',
'accepted_args' => 1,
),
'sanitize_email' =>
array (
'function' => 'sanitize_email',
'accepted_args' => 1,
),
'wp_filter_kses' =>
array (
'function' => 'wp_filter_kses',
'accepted_args' => 1,
),
),
)
次佳解決方案
要查看掛鈎到特定動作鈎子的功能或動作的列表,您可以使用以下代碼。
global $wp_filter;
echo '<pre>';
var_dump( $wp_filter['wp_head'] );
echo '</pre>';
第三種解決方案
對於 debug-purposes 一個簡單
global $wp_filter;
echo "<pre>" . print_r($wp_filter, true) . "</pre>";
會做…
第四種方案
這顯示了一個更可讀的過濾器列表
function print_filters_for( $hook = '' ) {
global $wp_filter;
if( empty( $hook ) || !isset( $wp_filter[$hook] ) ) return;
$ret='';
foreach($wp_filter[$hook] as $priority => $realhook){
foreach($realhook as $hook_k => $hook_v){
$hook_echo=(is_array($hook_v['function'])?get_class($hook_v['function'][0]).':'.$hook_v['function'][1]:$hook_v['function']);
$ret.= "n$priority $hook_echo";
}
}
return $ret;
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。