Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

As an example, I'm looking at class-wp-xmlrpc-server.php and there's a line (~115) that says:

$this->methods = apply_filters('xmlrpc_methods', $this->methods);

How can I actually find out what transformations are performed on the data by the filter? Obviously, I can compare $this->methods before and after the filter is applied to get the bottom line differences but I'd like to know what code is actually being run to make those changes.

Any ideas?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
The cap administrator can change, when role names get changed, added, etc.. 'manage_options' is much safer. +1 for a nice script. –  kaiser Mar 29 '12 at 11:47
    
Sidenotes: display_fired_filters() doesn't use GLOBALS['wp_filter'] anywhere. When using double quotes, then you can use $var inside the string: Instead of "<h1>".$index it could be "<h1>$index" or "<h1>{$index}". –  kaiser Mar 29 '12 at 12:30
    
:D Thanks Kaiser - I had been playing around with the snippet, getting it to do different things and never bothered tidying it up... –  Stephen Harris Mar 29 '12 at 12:44
    
Just took it and cleaned it up a bit. You can find it here. Hope you don't mind :) It's a mu-plugin. Oh, and I switched to &debug=true. –  kaiser Mar 29 '12 at 13:39

By interacting with the $wp_filter global, you can see all the filters that are setup at any given time. To save you a bit of time, our very own @Rarst has written some code which will do this for you, you can find it here. Make sure you execute this after the filters have been added, so that you don't end up missing anything crucial.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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