Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.