如何获取(循环以禁用不必要的)具有优先级的所有功能\'1\'
连接到挂钩\'wp_head\'
(\'_wp_render_title_tag\'
, \'wp_enqueue_scripts\'
, \'noindex\'
等等。)?
有必要自动获取它,以便在每个新版本的WordPress之后不跟踪优先级\'1\'
还有别的。
类似于:
$array_priority_1 = [];
if (doing_action(\'wp_head\')) {
// Here we get and iterate over all functions related to \'wp_head\'
// and immediately check their priority \'1\'?
// suitable and added to the array \'$array_priority_1\'
//
// ...I do not know how to get them and check for priority
}
下面我们将介绍我们的ready阵列:
foreach ($array_priority_1 as ...
谢谢大家!
SO网友:bueltge
全球$wp_filter
存储钩子,还有他的参数,包括优先级。因此,您可以创建一个小函数来调试它们。follow函数应该有助于列出所有函数,如果启动,则列出其参数wp_head
. foreach循环通过run和do添加钩子的优先级。因此可以列出挂钩的所有函数wp_head
以优先级10为例,通过var_dump($hooked[0][10]);
.
add_action(\'all\', function($hook) {
if (doing_action(\'wp_head\')) {
global $wp_filter;
if ( ! isset( $wp_filter[ $hook ] ) ) {
return;
}
$hooked = (array) $wp_filter[ $hook ];
foreach ( $hooked as $priority => $function ) {
// Prevent buffer overflows of PHP_INT_MAX on array keys.
// So reset the array keys.
$hooked = array_values( $hooked );
$hooked[] = $function;
}
// Print all functions with priority `1`.
print \'<pre>\'; print_r($hooked[0][1]); print \'</pre>\';
}
return $hook;
});
结果
对于我的测试示例,打印具有优先级的所有函数
1
Array
(
[_wp_render_title_tag] => Array
(
[function] => _wp_render_title_tag
[accepted_args] => 1
)
[wp_enqueue_scripts] => Array
(
[function] => wp_enqueue_scripts
[accepted_args] => 1
)
[noindex] => Array
(
[function] => noindex
[accepted_args] => 1
)
[wp_post_preview_js] => Array
(
[function] => wp_post_preview_js
[accepted_args] => 1
)
)
另外,调试助手插件中的这个函数应该可以帮助您理解它-
https://github.com/bueltge/debug-objects/blob/master/inc/class-page_hooks.php#L90