The question:
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( "nn", $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)?
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
Firstly, keep in mind that the structure of $wp_filter
was changed in WordPress 4.7: https://make.wordpress.org/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( "nn", $arr_vals )
will probably not produce the expected result.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0