Skip to content

Instantly share code, notes, and snippets.

@mayeenulislam
Forked from EmranAhmed/functions.php
Created February 7, 2017 06:38
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 mayeenulislam/79d81bdc8af2507251159f4d13a0f3ef to your computer and use it in GitHub Desktop.
Save mayeenulislam/79d81bdc8af2507251159f4d13a0f3ef to your computer and use it in GitHub Desktop.
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

Hook Info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment