Enumerate and count all hooks and actions triggered in WordPress

Think of hooks & actions in WordPress development like of train stations & passengers: at any hook an action can be loaded (add_action) or unloaded (remove_action); and when the control comes, that’s a filter (add_filter). At the end of the journey the web page is completed and ready to go into the users’ browser.

Problem is the stations and passengers are different for each page of each web site, and given the flexibility of the hooks system and the multitude of plugins and themes, sometimes is dificult to spot the right hook for loading an action.

So at times it’s useful to know the entire hooks map for a given page; that’s pretty simple to do with a function:

add_action( 'shutdown', function(){
  if( current_user_can('administrator') ) {
    foreach( $GLOBALS['wp_actions'] as $action => $count )
      printf( '%s (%d) <br/>' . PHP_EOL, $action, $count );
  }
});

Add this in functions.php of your CHILD theme, refresh the page and scroll to the end to see the list. Only works when logged in as Administrator – though this alters a bit the hooks lists, including the admin related ones.

Leave a Reply