1

I am developing a Plugin, and want to record Time taken by each Action Hook, I am able to get all the Hooks Triggered on a Page with the Action and Priority, but i am unable to get the Time taken by each of them for their Full Execution. The Code to list all the Triggered Action Hooks is given as :

`

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);

` Thanks in advance.

  • Hmm... And what exactly are you trying to achieve? Because your code doesn't even try to solve the problem you stated in description... And it's rather harmful, I'm afraid, since it iterates through all hooks during every hook call, so it will take some time... – Krzysiek Dróżdż Dec 11 '18 at 18:40
  • @KrzysiekDróżdż I am new to Plugin Development and Want help from Developers like you. Above written code works the way you are describing but I also want to track the Time taken by each Hook. Hope you got it. – Akash Manhas Dec 13 '18 at 6:43

Your Answer

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

Browse other questions tagged or ask your own question.