0

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?

4

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

1

I wrote this plugin to do what you're asking - Simply Show Hooks. It shows you where all the action and filter hooks are, inline, in order, on the page you're looking at, and all the functions that have been hooked to them. Hope this helps :)

Simply Show Hook - WordPress plugin shows you where all the action and filter hooks are, inline, in order, on the page you're looking at

  • I've been using this for several years. Great plugin @Stuart OB – Phill Healey Apr 12 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.