Last active
December 20, 2015 06:09
-
-
Save WPsites/6083397 to your computer and use it in GitHub Desktop.
Function to inspect WordPress filters/actions that match a regular expression
This file contains hidden or 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
<?php | |
//hook into the 'shutdown' action and debug some filters... but not on the admin this time | |
if (! is_admin() ) | |
add_action('shutdown', 'use_debug_filters',10); | |
function use_debug_filters(){ | |
echo "<h3>Output hooks and filters that contain the string 'init'</h3>"; | |
debug_filter("/init/i"); | |
echo "<h3>Output hooks and filters that contain the string 'before'</h3>"; | |
debug_filter("/before/i"); | |
echo "<h3>Output hooks and filters that start with 'wp'</h3>"; | |
debug_filter("/^wp/i"); | |
} | |
/** | |
* Loop through $wp_filter array and output any actions/filters that match the regular expression passed in. | |
*/ | |
function debug_filter($regex_filter_action_to_match = "//"){ | |
global $wp_filter; | |
$hook=$wp_filter; | |
ksort($hook); | |
echo "<pre style='background-color:white;'>"; | |
foreach($hook as $tag => $priority){ | |
//check for a regex match of hook name | |
if ( !preg_match($regex_filter_action_to_match, $tag) ) continue; | |
echo "<br /><strong>$tag</strong><br />"; | |
ksort($priority); | |
foreach($priority as $priority => $function){ | |
echo " $priority \n"; | |
foreach($function as $name => $properties) { | |
echo "\t$name "; | |
if ( function_exists($name) ){ | |
$func = new ReflectionFunction($name); | |
$filename = $func->getFileName(); | |
$start_line = $func->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block | |
$path_info = pathinfo($filename); | |
echo "<em style='font-size:90%;opacity:0.5;' title='{$filename}'> {$path_info[filename]}.{$path_info[extension]}: line {$start_line}</em>"; | |
} | |
echo "<br />"; | |
} | |
} | |
} | |
echo '</pre>'; | |
return; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment