variables

How To: Get an Overview of all Action and Filter Hooks

April 17, 2009   ·   By   ·   No Comments   ·   Posted in Functions, Wordpress, Writing Plugins

Here’s a very cool little tidbit I found today (strangely enough, I found it in the bbpress codebase, while working on a bbpress plugin..)  WordPress keeps track of which function/filter combinations are registered, along with which action hooks have been called at any point.  If you’re having trouble trying to figure out which action hook you should use to run some code for your plugin or theme, give this a try (on a test install, of course).

$wp_filter()

As far as I can tell, this holds every currently registered function/filter combination, and all the relevant information.  I can’t quite figure out exactly what determines which filters this grabs – I’ve tried calling it from a few places, and the results are slightly different, but I can’t seem to find a pattern. I initially assumed that you wouldnt be able to see about admin side hooks from the front end, but this isn’t the case. At any rate, you get lots of information from anywhere. Not getting the info you want? Call it from somewhere else! This is a great debugging tool if you’re having trouble with plugin incompatibility – it gives you a list of the filters and actions being called,the matching functions that are working on them, the priority specified for each hook call, the accepted args, and even the order in which same-priority-level hooks are called.  An example:

    [the_title] => Array
        (
            [10] => Array
                (
                    [wptexturize] => Array
                        (
                            [function] => wptexturize
                            [accepted_args] => 1
                        )

                    [convert_chars] => Array
                        (
                            [function] => convert_chars
                            [accepted_args] => 1
                        )

                    [trim] => Array
                        (
                            [function] => trim
                            [accepted_args] => 1
                        )

                )

        )

This is a small snippet I pulled after displaying the contents of this variable in the footer of a theme.  As you can see, the referenced hook is a filter, specifically “the_title”.  It has 3 functions attached to it: wptexturize, convert_chars, and trim.  Each one is at priority level 10 (which is the default), but they are called in the order listed.   Each accepts 1 argument.

Try it Out

If you want to have a look yourself, display the contents of wp_filter with this code snippet:

<?php
global $wp_filter;
print_r($wp_filter);
?>

This is going to dump the contents right to the screen, so you don’t want to do it on a live blog. As with any use of the print_r() function, the results will be formatted nicely in the source code – but not in the rendered html.

Roundup:
Top wordpress plugins
wordpress magazine theme