Check if particular action or filter exists in a WordPress theme

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.

Related posts

Leave a Reply

1 comment

  1. 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.