Is there a way to know what functions are hooked to a particular hook? For example if I’d like to know what functions are hooked to the wp_head hook.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Look into the global variable $wp_filter. See my plugin for a list of all comment filters for an example:
<?php
/*
Plugin Name: List Comment Filters
Description: List all comment filters on wp_footer
Version: 1.1
Author: Fuxia Scholz
License: GPL v2
*/
add_action( 'wp_footer', 'list_comment_filters' );
function list_comment_filters()
{
global $wp_filter;
$comment_filters = array ();
$h1 = '<h1>Current Comment Filters</h1>';
$out = '';
$toc = '<ul>';
foreach ( $wp_filter as $key => $val )
{
if ( FALSE !== strpos( $key, 'comment' ) )
{
$comment_filters[$key][] = var_export( $val, TRUE );
}
}
foreach ( $comment_filters as $name => $arr_vals )
{
$out .= "<h2 id=$name>$name</h2><pre>" . implode( "nn", $arr_vals ) . '</pre>';
$toc .= "<li><a href='#$name'>$name</a></li>";
}
print "$h1$toc</ul>$out";
}
Sample output for pre_comment_author_email:
array (
10 =>
array (
'trim' =>
array (
'function' => 'trim',
'accepted_args' => 1,
),
'sanitize_email' =>
array (
'function' => 'sanitize_email',
'accepted_args' => 1,
),
'wp_filter_kses' =>
array (
'function' => 'wp_filter_kses',
'accepted_args' => 1,
),
),
)
Method 2
to see list of functions or actions hooked to a particular action hook you can use the following code.
global $wp_filter; echo '<pre>'; var_dump( $wp_filter['wp_head'] ); echo '</pre>';
Method 3
For debug-purposes a simple
global $wp_filter; echo "<pre>" . print_r($wp_filter, true) . "</pre>";
would do it …
Method 4
This shows a more readable list of filters
function print_filters_for( $hook = '' ) {
global $wp_filter;
if( empty( $hook ) || !isset( $wp_filter[$hook] ) ) return;
$ret='';
foreach($wp_filter[$hook] as $priority => $realhook){
foreach($realhook as $hook_k => $hook_v){
$hook_echo=(is_array($hook_v['function'])?get_class($hook_v['function'][0]).':'.$hook_v['function'][1]:$hook_v['function']);
$ret.= "n$priority $hook_echo";
}
}
return $ret;
}
Method 5
I found the answer from @Simone G useful, but it didn’t take into account the fact, that sometimes Closures can be hooked. Here’s my more verbose (and ugly) version:
if( isset($wp_filter[$filterName]) ){
foreach( $wp_filter[$filterName] as $priority => $hooks){
foreach ($hooks as $hook_k => $hook_v) {
$hook_echo=(is_array($hook_v['function'])?get_class($hook_v['function'][0]).':'.$hook_v['function'][1]:$hook_v['function']);
if(is_object($hook_echo) && ($hook_echo instanceof Closure)){
$hook_echo="closure";
}
error_log($filterName." HOOKED (".serialize($priority)."): ".serialize($hook_k)."".serialize($hook_echo));
}
}
} else {
error_log($filterName." NO FILTERS HOOKED");
}
Method 6
It saves my time because I can see direcly in any page https://wordpress.org/plugins/show-hooks/
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0