admin管理员组文章数量:1291041
I'm trying to understand the internals of the $wp_filter
object and I'm looking at the code posted by Fuxia in this question How to know what functions are hooked to an action/filter?
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( "\n\n", $arr_vals ) . '</pre>';
$toc .= "<li><a href='#$name'>$name</a></li>";
}
print "$h1$toc</ul>$out";
}
As far as I can tell the code in the first foreach
loop, and the call to implode
in the second foreach
handle a case where the $wp_filter
object contains two entries with same key. So my questions are
- Is this possible?
- Under what circumstances would this occur?
Edit
Following the answer from Jacob Peattie I can see that $wp_filter['save_post']
could hold multiple values, to phrase my question in another way, in this section of the code
foreach ( $wp_filter as $key => $val )
{
if ( FALSE !== strpos( $key, 'comment' ) )
{
$comment_filters[$key][] = var_export( $val, TRUE );
}
}
is there ever a scenario when the same $key
value could be encountered twice? I suspect that
$comment_filters[$key][] = var_export( $val, TRUE );
could be changed to
$comment_filters[$key] = var_export( $val, TRUE );
and the results the function returns would always be the same.
Or is there a scenario where $wp_filter
could contain two elements with the same key (not one element that holds an array/WP_Hook with references to multiple functions)?
I'm trying to understand the internals of the $wp_filter
object and I'm looking at the code posted by Fuxia in this question How to know what functions are hooked to an action/filter?
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( "\n\n", $arr_vals ) . '</pre>';
$toc .= "<li><a href='#$name'>$name</a></li>";
}
print "$h1$toc</ul>$out";
}
As far as I can tell the code in the first foreach
loop, and the call to implode
in the second foreach
handle a case where the $wp_filter
object contains two entries with same key. So my questions are
- Is this possible?
- Under what circumstances would this occur?
Edit
Following the answer from Jacob Peattie I can see that $wp_filter['save_post']
could hold multiple values, to phrase my question in another way, in this section of the code
foreach ( $wp_filter as $key => $val )
{
if ( FALSE !== strpos( $key, 'comment' ) )
{
$comment_filters[$key][] = var_export( $val, TRUE );
}
}
is there ever a scenario when the same $key
value could be encountered twice? I suspect that
$comment_filters[$key][] = var_export( $val, TRUE );
could be changed to
$comment_filters[$key] = var_export( $val, TRUE );
and the results the function returns would always be the same.
Or is there a scenario where $wp_filter
could contain two elements with the same key (not one element that holds an array/WP_Hook with references to multiple functions)?
1 Answer
Reset to default 2Firstly, keep in mind that the structure of $wp_filter
was changed in WordPress 4.7: https://make.wordpress/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
As for your questions:
Is this possible?
Yes.
Under what circumstances would this occur?
If multiple functions have been hooked to the same action/filter, eg:
add_action( 'save_post', 'do_a_thing' );
add_action( 'save_post', 'do_another_thing' );
In that case $wp_filter['save_post']
will have multiple values.
To address your edit:
is there ever a scenario when the same $key value could be encountered twice? I suspect that
$comment_filters[$key][] = var_export( $val, TRUE );
could be changed to
$comment_filters[$key] = var_export( $val, TRUE );
No, it's not possible for the same key to be encountered twice. PHP arrays cannot have the same key more than once.
Your amended code is correct. In fact, without that change implode( "\n\n", $arr_vals )
will probably not produce the expected result.
本文标签: filtersCan the wpfilter object hold multiple values with the same key
版权声明:本文标题:filters - Can the wp_filter object hold multiple values with the same key 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741518640a2383046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论