Find What and Where are Each Function in a WordPress Hook (What File & Line)

Ok, This might be the most useful function I’ve ever used for debugging WordPress. This will help out tremendously when you have some ghost tags that you don’t know where it comes from.

The idea here is to go through each function in a hook, and find out where that function is. That way, you can determine where to fix whatever you need to fix. This is so useful, I might make it into a plugin. We’ll see if I have time. For now, here is some code you can add to your theme temporarily while you debug.

$tag='woocommerce_after_main_content';   //Add the name of the hook here
global $wp_filter;
if ($tag) {
	$hook[$tag]=$wp_filter[$tag];
	if (!is_array($hook[$tag])) {
		trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
		return;
	}
}
echo '
';
foreach($hook as $tag => $priority) {
	echo "
$tag:
    "; ksort($priority); foreach($priority as $priority => $function) { echo '
  • '.$priority.'
      '; foreach($function as $name => $properties) { $reflFunc = new ReflectionFunction($name); echo '
    • '.$name.' | '.$reflFunc->getFileName() . ' | Line:' . $reflFunc->getStartLine().'
    • '; } echo '
  • '; } echo '
      '; } echo '
';

Leave a comment

Your email address will not be published. Required fields are marked *