1

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 1

2

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( "\n\n", $arr_vals ) will probably not produce the expected result.

5
  • Thanks for the answer, so based on that link, if I'm running the code on a version of WordPress > 4.7 $wp_filter['save_post'] will be an instance of WP_Hook and will not have multiple values. But for previous WordPress versions it would be an array with multiple values. Is that correct?
    – ipr101
    Jun 7, 2021 at 8:48
  • Yes, but WP_Hook implements the ArrayAccess and Iterator, so in practice there’s not much difference. Jun 7, 2021 at 9:38
  • Thanks again , so after WordPress 4.7 the wp_filter object no longer contains multiple entries with the same key, it will contain a single entry of type WP_Hook, that can be iterated over to find the linked functions. Is that right?
    – ipr101
    Jun 7, 2021 at 10:48
  • I've checked a WordPress version prior to 4.7 and I can see how $wp_filter['save_post'] is an array instead of a WP_Hook object but all the data is still held in a single place rather than across multiple places in the $wp_filter object that share the same key. I've updated my question as I'm not sure if it was clear what I was asking in the first edit.
    – ipr101
    Jun 7, 2021 at 13:31
  • I've updated my answer. Jun 7, 2021 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.