How can I find out what an `apply_filter` call is actually doing?

Here is a snippet, similar to the above which prints out the hooked functions for all (or a specified) hook, every time it is called. The advantage is that this will capture (more, not all) ‘dynamically’ added hooks. In particular it will display only functions currently hooked onto that hook at the time of calling. (Sometimes a function will be removed or added to a hook for a particular instance). On the other hand it requires the hook to be called.

The snippet works for actions and filters (they are essentially the same thing) – but like the above can only give you priority, name and number of arguments. To find out what the functions themselves are doing you’ll have to find them…

Usage: log-in as admin user. Visit any page, front or back, with GET parameters set as debug=secret and (optionally) hook=hook_name.

This snippet is for development only.

if( current_user_can('administrator') && !empty($_GET[ 'debug' ]) && 'secret' == $_GET[ 'debug' ] ){
    global $sh_fired_filters;
    $sh_fired_filters = array();
    add_action('all','store_fired_filters');
    add_action( 'shutdown' , 'display_fired_filters' );
}

function store_fired_filters($tag){
    if(!empty($_GET[ 'hook' ]) && $_GET[ 'hook' ]!= $tag)
        return;

    global $wp_filter;
    global $sh_fired_filters;

    if( ! isset($wp_filter[$tag]))
        return;

    $hooked = $wp_filter[$tag];

    ksort($hooked);

    foreach ($hooked as $priority => $function):
            $hooked[] = $function;
    endforeach;
    $sh_fired_filters[] = array('tag'=>$tag, 'hooked' => $wp_filter[$tag]);
}


function display_fired_filters(){
    global $sh_fired_filters;
    global $wp_filter;
    foreach($sh_fired_filters as $index=> $the_):   
            echo "<h1>".$index.' '.$the_['tag']."</h1>";    
            echo "<ul>";
            foreach($the_['hooked'] as $priority => $hooked):
                foreach($hooked as $id => $function):
                    echo '<li>'.$priority.' <strong>'.print_r($function['function'],true).'</strong> (accepted args:'.$function['accepted_args'].')</li>';
                endforeach;
            endforeach;
            echo "</ul>";

    endforeach;
}

This will display only hooks that have been fired in the course of the page load.

Leave a Comment