Show Which Functions Run on a Hook
Lets administrators add ?wpide_hook=the_content (or any hook name) to a URL and see the callbacks attached to that hook with their priorities and source files, printed at the bottom of the page.
Opens WPIDE on
Code
add_action( 'shutdown', function () {
global $wp_filter;
if ( empty( $_GET['wpide_hook'] ) || ! current_user_can( 'manage_options' ) || wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
return;
}
$hook = sanitize_text_field( wp_unslash( $_GET['wpide_hook'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
echo '<div id="wpide-hook-inspector" style="position:relative;z-index:99999;margin:20px;padding:16px;background:#fff;border:2px solid #2271b1;font:13px/1.5 monospace;color:#1e1e1e;">';
echo '<strong>Callbacks on "' . esc_html( $hook ) . '"</strong><br>';
if ( empty( $wp_filter[ $hook ] ) ) {
echo 'None.</div>';
return;
}
foreach ( $wp_filter[ $hook ]->callbacks as $priority => $callbacks ) {
foreach ( $callbacks as $callback ) {
$fn = $callback['function'];
$label = 'closure';
$file = '';
try {
if ( is_string( $fn ) ) {
$label = $fn;
$ref = strpos( $fn, '::' ) ? new ReflectionMethod( $fn ) : new ReflectionFunction( $fn );
} elseif ( is_array( $fn ) ) {
$label = ( is_object( $fn[0] ) ? get_class( $fn[0] ) : $fn[0] ) . '::' . $fn[1];
$ref = new ReflectionMethod( $fn[0], $fn[1] );
} else {
$ref = new ReflectionFunction( $fn );
}
$file = str_replace( ABSPATH, '', (string) $ref->getFileName() ) . ':' . $ref->getStartLine();
} catch ( ReflectionException $e ) {
$file = '';
}
printf( '%1$d %2$s <span style="color:#646970;">%3$s</span><br>', (int) $priority, esc_html( $label ), esc_html( $file ) );
}
}
echo '</div>';
}, 0 );
Notes
Only administrators see the output. Remove the parameter to hide it again.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.