Back to Top

Check if a Hook is being Filtered

Previous Post:

Check if a Hook is being Filtered

Since version 2.5.0 WordPress has provided a handy function named has_filter()1 that loops through the $wp_filter global variable and check if any filter has been registered for a particular hook.

Despite the fact that this function is not used much in plugin development, it can actually be very useful. Imagine two plugins that have completely different goals but share some parts of their codes, and both have to filter the_content hook with similar functions, for example, formatting(). Using has_filter() , one plugin can easily avoid having to run the same function twice:

/* 'another_plugin_formatting' and 'my_plugin_formatting' are
 different functions but have similar objectives. */
if (!has_filter('the_content', 'another_plugin_formatting'))
	add_filter('the_content', 'my_plugin_formatting');

Simple, but is a huge memory and processing time saver! And if you want to do something evil to the other plugin (which is my personal favourite thing =P), try this:

$priority = has_filter('the_content', 'another_plugin_formatting');
if ($priority)
// Please note that you have to use remove_filter with
// appropriate arguments, as per the codex2
	remove_filter('the_content', 'another_plugin_formatting', $priority);
add_filter('the_content', 'my_plugin_formatting');

Of course you should add this snippet to a function that hooks to do_action('init') with a rather high priority to make sure that it is called after the other plugin, otherwise the filter that gets removed might be yours instead. Also note that this might fail to work because of a bug in remove_filter(), which I have written about here, so take a look if you are interested :).


  1. ... has_filter []
  2. ... ove_filter []

Take Social Sharing to
the Next Level with Monarch!

Take Social Sharing to the Next Level with Monarch!
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS

5 Opinions for Check if a Hook is being Filtered

  1. User's Gravatar
    Lena February 9, 2011 at 3:13 pm – Permalink

    Never thought this was possible…thanks!

  2. User's Gravatar
    Morze August 22, 2011 at 10:38 pm – Permalink

    whoah this blog is fantastic i love reading your posts. Keep up the great work! You know, lots of people are searching around for this information, you could aid them greatly.

  3. User's Gravatar
    Rutwick December 17, 2011 at 2:24 am – Permalink

    Thanks for the trick. I’m having a hard time solving a conflict between my plugin and some other plugin, both of us use the_content and the_excerpt filters to append additional content. Your trick solved the issue with the_excerpt, because I could find out which function was applied to this filter. Now this is a very bad way as I had hard coded the function name. There could be other plugins with many functions applied to the_content out there!
    I have a question, is there a way, using which I can find out which functions are attached with a specific filter? So that I can use those names in conjunction with your method to avoid the conflict!
    – Rutwick

    • User's Gravatar
      OddOneOut December 18, 2011 at 10:44 am – Permalink

      Hi Rutwick,

      You can view the contents of the $wp_filter global variable, it will tell you what hook is being filtered and what functions are filtering it.

      • User's Gravatar
        Rutwick December 19, 2011 at 10:32 am – Permalink

        It’s a great way to find out the filter functions!
        – Rutwick

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.

  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: