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,
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 :).