Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active August 7, 2023 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/fbf22063a74c7844c85ee2a8013b934e to your computer and use it in GitHub Desktop.
Save westonruter/fbf22063a74c7844c85ee2a8013b934e to your computer and use it in GitHub Desktop.
Install as mu-plugin to get dump of all hooks invoked more than once.
<?php
/**
* Plugin Name: Hook Counter
* Author: Weston Ruter
* Description: Drop in mu-plugins and see a markdown table at the end of a page's source code counting all hooks invoked more than once.
*/
$hook_counts = [];
add_filter( 'all', static function ( $hook_name ) use ( &$hook_counts ) {
if ( ! isset( $hook_counts[ $hook_name ] ) ) {
$hook_counts[ $hook_name ] = 1;
} else {
$hook_counts[ $hook_name ]++;
}
}, PHP_INT_MIN );
add_action( 'shutdown', static function () use ( &$hook_counts ) {
arsort( $hook_counts );
echo "\n<!--\n";
echo "Hook | Count\n";
echo "-- | --:\n";
foreach ( $hook_counts as $hook_name => $hook_count ) {
if ( $hook_count > 1 ) {
printf( "%s | %s\n", $hook_name, number_format( $hook_count ) );
}
}
echo "-->\n";
}, PHP_INT_MAX );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment