WordPress Utility: List all functions mounted to hooks and filters
In view of some theme code, we often look confused, named code is very clean, no special function function, but the foreground output code is more than some of the things we do not want, which is likely to be the theme author of the corresponding function mounted to the WordPress hook above. But in the end is hung on which hook, or a hook in the end on which functions are mounted, little by little code to find is too slow, today to share a more convenient way.
List all functions mounted to hooks and filters
/**
* Print Filters For
*
* Discover what functions are attached to a given hook in WordPress.
*/
function print_filters_for( $hook = null ) {
global $wp_filter;
// Error handling
if ( !$hook )
return new WP_Error( 'no_hook_provided', __("You didn't provide a hook.") );
if ( !isset( $wp_filter[$hook] ) )
return new WP_Error( 'hook_doesnt_exist', __("$hook doesn't exist.") );
// Display output
echo ''; echo "How to get a new WP_Error?
echo "Hook summary: $hook
'; echo "<pre style="Hook_does_exist', __('$hook doesn't exist.
echo '';
print_r( $wp_filter[$hook] );
echo '
';
echo '
';
}
Usage
For example, the theme I'm using has a custom hook.
/* Show breadcrumb navigation */
function wizhi_show_breadcrumb() {
if ( function_exists( 'yoast_breadcrumb' ) ) {
yoast_breadcrumb( '<p class="breadcrumbs">Current position: ', '</p>' );
}
}
add_action( 'mx_post_before', 'wizhi_show_breadcrumb' );
Now, I need to show the functions mounted to this hook, just add the following code inside the function.
print_filters_for('mx_post_before');
The results were displayed as follows:
Hook Summary: mx_post_beforeArray
(
[10] => Array
(
[show_breadcrumb] => Array
(
[function] => show_breadcrumb // Here is the function that is mounted to that hook.
[accepted_args] => 1
)
)
)
With this method, it helps us a lot to understand the operation mechanism of WordPress themes and plugins, and it will be much easier to do WordPress development. If you have a better way, welcome to share it in the comments.