Last active
August 29, 2015 14:16
-
-
Save hellofromtonya/fa4201d18dd7c0f84acf to your computer and use it in GitHub Desktop.
WordPress - Print out a list of all the hooked functions for a given tag (or all if no tag is passed). Builds a table showing rowed by priority.
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
/** | |
* An example of how to call it | |
*/ | |
add_action( 'wp_head', function() { | |
print_list_hooked_functions( 'genesis_header' ); | |
} ); | |
/** | |
* Print a list of all hooked functions | |
* | |
* @since 1.0.0 | |
* | |
* @param string $tag (optional) The name of the action to be evaluated. | |
* Leave it blank to print all hooks. | |
* @return void | |
*/ | |
function print_list_hooked_functions( $tag = '' ) { | |
global $wp_filter; | |
if ( $tag ) { | |
$hook[ $tag ] = isset( $wp_filter[ $tag ] ) ? $wp_filter[ $tag ] : null; | |
//* If the $tag does not exist, then print a message and bail out. | |
if ( empty( $hook[ $tag ] ) || ! is_array( $hook[ $tag ] ) ) { | |
printf( '<p>%s %s.</p>', __( 'No hooks found for', 'lunarwp' ), $tag ); | |
return; | |
} | |
//* We'll do all of them | |
} else { | |
$hook = $wp_filter; | |
ksort( $hook ); | |
} | |
//* Loop through each of the hooks and print them out | |
foreach ( $hook as $tag => $hooked_functions ) { | |
//* print the first level | |
printf( '<p style="margin-left: 40px;"><strong>%s</strong><br>', esc_html( $tag ) ); | |
echo '<table style="margin-left: 40px">'; | |
printf( '<thead><tr><th style="text-transform: capitalize;">%s</th><th style="text-transform: capitalize;">%s</th></tr></thead><tbody>', | |
__( 'Priority', 'lunarwp' ), | |
__( 'Hooked Function Name', 'lunarwp' ) | |
); | |
ksort( $hooked_functions ); | |
//* Loop through the priorities and functions | |
foreach ( $hooked_functions as $priority => $functions ) { | |
//* Print the priority level | |
printf( '<tr><td width="100px">%s</td><td>', esc_html( $priority ) ); | |
$function_names = array_keys( $functions ); | |
sort( $function_names ); | |
echo implode( '<br>', $function_names ); | |
echo '</td></tr>'; | |
} | |
echo '</tbody></table></p>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment