問題描述
我有一個關於 WordPress 的問題,特別是版本 3.0 和更新版本。
有沒有人知道如何獲取將要應用的所有函式的陣列或列表,或者是’registered’ 到 the_content 過濾器?
這個想法是生成一個可以從過濾器中刪除的函式的核取方塊列表,如 wpautop 。我知道如何使用硬編碼標籤從過濾器中刪除功能,但我希望建立一個更具動態性的解決方案。
如果任何人有任何想法,如果這是可能的,如何做到這一點我會很感興趣。謝謝。
最佳解決思路
從過濾器陣列列印的簡單功能?
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 陣列的資料外,還將顯示連線鉤子的檔案的路徑,以及定義函式的程式碼行。
要獲取掛接在特定操作 (或過濾器) 上的函式的基本列表,就足以從過濾器陣列中獲取專案,但是由於函式可以以各種方式附加 (作為類方法或閉包),該列表將包含一噸的有關資料,包括呈現為字串的物件。該示例將僅按照優先順序順序顯示相關資料:
-
函式名 (取決於回撥語法):
-
函式回撥:
'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 操作是獲取完整列表的好地方)
print_r the_content 過濾器示例:
Array
(
[0] => Array
(
[id] => 000000004c8a4a660000000011808a14run_shortcode
[priority] => 8
[function] => Array
(
[0] => WP_Embed
[1] => run_shortcode
)
[accepted_args] => 1
[file] => C:xampphtdocswordpresswp-includesclass-wp-embed.php
[line] => 58
)
[1] => Array
(
[id] => wptexturize
[priority] => 10
[function] => wptexturize
[accepted_args] => 1
[file] => C:xampphtdocswordpresswp-includesformatting.php
[line] => 41
)
[2] => Array
(
[id] => 0000000006c5dc6d0000000064b1bc8e
[priority] => 10
[function] => Closure
[accepted_args] => 1
[file] => C:xampphtdocswordpresswp-contentpluginspluginplugin.php
[line] => 16
)
.....
編輯:2017-05-05
-
適用於
WP_Hook類 -
增加優先權
-
固定:如果回撥不存在則引發錯誤,儘管 WordPress 也引發了警告
-
固定:具有相同 ID 但不同優先順序的鉤子覆蓋前一個
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。