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)?

Share Improve this question edited Jun 7, 2021 at 13:33 ipr101 asked Jun 7, 2021 at 8:07 ipr101ipr101 1135 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Firstly, 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