Last active
August 7, 2023 17:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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