ArRolin
2/6/2017 - 4:56 PM

Hook Info. Get Action hook info. What functions are hooked to an action / filter in WordPress? How can I see all the actions attached to an

Hook Info. Get Action hook info. What functions are hooked to an action / filter in WordPress? How can I see all the actions attached to an "add_action" hook?

<?php
     if ( ! function_exists( 'hippo_plugin_hook_info' ) ):

        function hippo_plugin_hook_info( $hook_name ) {
            global $wp_filter;

            $docs     = array();
            $template = "\t - %s Priority - %s.\n\tin file %s #%s\n\n";

            echo '<pre>';
            echo "\t# Hook Name \"" . $hook_name . "\"";
            echo "\n\n";
            if ( isset( $wp_filter[ $hook_name ] ) ) {
                foreach ( $wp_filter[ $hook_name ] as $pri => $fn ) {

                    foreach ( $fn as $fnname => $fnargs ) {

                        if ( is_array( $fnargs[ 'function' ] ) ) {
                            $reflClass = new ReflectionClass( $fnargs[ 'function' ][ 0 ] );
                            $reflFunc  = $reflClass->getMethod( $fnargs[ 'function' ][ 1 ] );
                            $class     = $reflClass->getName();
                            $function  = $reflFunc->name;
                        } else {
                            $reflFunc  = new ReflectionFunction( $fnargs[ 'function' ] );
                            $class     = FALSE;
                            $function  = $reflFunc->name;
                            $isClosure = (bool) $reflFunc->isClosure();
                        }

                        if ( $class ) {
                            $functionName = sprintf( 'Class "%s::%s"', $class, $function );
                        } else {
                            $functionName = ( $isClosure ) ? "Anonymous Function $function" : "Function \"$function\"";
                        }

                        printf( $template, $functionName, $pri, str_ireplace( ABSPATH, '', $reflFunc->getFileName() ), $reflFunc->getStartLine() );

                        $docs[] = array( $functionName, $pri );
                    }
                }

                echo "\tAction Hook Commenting\n\t----------------------\n\n";
                echo "\t/**\n\t* " . $hook_name . " hook\n\t*\n";
                foreach ( $docs as $doc ) {
                    echo "\t* @hooked " . $doc[ 0 ] . " - " . $doc[ 1 ] . "\n";
                }
                echo "\t*/";
                echo "\n\n";
                echo "\tdo_action( '" . $hook_name . "' );";

            }
            echo '</pre>';
        }
    endif;

Usage of Hook Info.

  • It's useful for plugin or theme documentation and for developer who want to know how many add_action attached with a specific do_action hook, which file with line number.

  • Just before any do_action hook like:

<?php

hippo_plugin_hook_info( 'woocommerce_before_shop_loop' ); // this will show woocommerce_before_shop_loop hook info

do_action( 'woocommerce_before_shop_loop' );

//...

Output