How To: List Filters That You Are About to Apply

I was working on a project where I had to find out all the registered filters that I was about to apply, so here’s a WordPress quickie for you! I found this out by inspecting the apply_filters function inside the plugin.php core file, and apparently there’s a $wp_filter global that contains them all in an associative array where keys are tags.

So by doing a print_r at a certain time in your theme or function, you’re able to find out the whole list, but you might as well filter to the hook that you’re looking for. As an example let’s take “comment_text” as the filter tag and list all it’s callbacks — i.e. let’s find out what filters are applied to the comment text before we print it out. Note that this has to run at a point where the filters have already been added, probably before a call to the comment_text() function.

 global $wp_filter;
 print_r( $wp_filter['comment_text'] );

This may seem pretty useless at first sight, but did you know that the comment_text is passed through wptexturize, convert_chars, make_clickable, convert_smilies and several other filters? I didn’t, and I used this trick to find out, which lead me to the make_clickable function which I was looking for in the first place ;)

If you’re wondering what the array keys are in the array that you’ve printed, those are the priorities which were passed (or defaulted) when calling add_filter.

I hope this turns out to be useful to somebody, so thanks for retweeting!

4 thoughts on “How To: List Filters That You Are About to Apply

  1. This is indeed helpful for developing and debugging, but no guarantees we don't change how this is stored in the future. As in, make sure you don't rely on this in your code :) It actually changed not too long ago for performance reasons.

    • Good point Andrew, and thanks for your comment. It's perfectly reasonable to refactor, change and performance-tune the core code, but hey, we're all here to adapt aren't we? Besides, I don't see a reason why somebody would rely on the snippet other than for debugging purposes ;)

      Cheers and thanks again!

  2. Very useful Konstantin, thank you. Especially being able to see priorities listed out like that. Takes a lot of the guesswork and sleuthing out of assigning priorities.

    Combining this snippet with ideas from Joost, Andrew, Ozh and Nexik on, here is a snippet using query variable to scope which hook to print filters for, which works only for admin. Also wraps result in pre tag. I would post the snippet here but am unsure what formatting may do to it. Thanks again for the tip.

Comments are closed.