Check if particular action or filter exists in a Wordpress theme

php, wordpress

Solution

There is not an built-in Wordpress function that will check for this. But you would be able to check if a filter exists using the following code:

// check for the existence of "the_content" filter
if( array_key_exists( 'the_content' , $GLOBALS['wp_filter']) ) {
}

The problem with this is that it will only check if the filter exists at the point at which the above code runs. So if the above code runs in a plugin, and a filter is added in a theme template file (which happens later in execution order, it won't show up yet.

Problem

I want to check if any action/filter exists in the Wordpress theme. I have tried `has_action` and `has_filter`. But the problem with these functions is that if the hook/filter exists and no callbacks are added to the hook or all callbacks are removed from the hook, these will return false.

Original source