Is it possible to track down Actions and Filters?

As far as I know using Actions and Filters hooks is the best approach for developing on WordPress. I have just to ask if there is any way to track down which Actions and Filters are applied on WordPress rendering.

My question is based on the fact that hooks may be applied by on WordPress through various plugins or the active theme itself.

Is there any way to track down which Actions and Filters hooks are fired and from which file are they called?

3 Answers
3

Know which file call a filter or an action is not possible running code, you have to search and read the source code or use some documentation.

There are different plugins that help you to debug the hook being fired in a page, search codex and/or Google for them.

However, to have a quick idea, add this snippet in you functions.php

class MyTracker {

  static $hooks;

  static function track_hooks( ) {
    $filter = current_filter();
    if ( ! empty($GLOBALS['wp_filter'][$filter]) ) {
      foreach ( $GLOBALS['wp_filter'][$filter] as $priority => $tag_hooks ) {
        foreach ( $tag_hooks as $hook ) {
          if ( is_array($hook['function']) )  {
            if ( is_object($hook['function'][0]) ) {
              $func = get_class($hook['function'][0]) . '->' . $hook['function'][1];
            } elseif ( is_string($hook['function'][0]) ) {
              $func = $hook['function'][0] . '::' . $hook['function'][1];
            }
          } elseif( $hook['function'] instanceof Closure ) {
            $func="a closure";
          } elseif( is_string($hook['function']) ) {
            $func = $hook['function'];
          }
          self::$hooks[] = 'On hook <b>"' . $filter . '"</b> run <b>'. $func . '</b> at priority ' . $priority;
        }
      }
    }
  }

}

add_action( 'all', array('MyTracker', 'track_hooks') );

add_action( 'shutdown', function() {
    echo implode( '<br />', MyTracker::$hooks );
}, 9999);

now visit the target page and scroll down to the bottom…

Leave a Comment