Last active
November 26, 2018 14:37
-
-
Save maxchirkov/b3391f4d23e9c219b66d1a1ba9699805 to your computer and use it in GitHub Desktop.
Dumps WP Hooks (Filters and Actions) in the order of execution in Markdown format.
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
/** | |
* Dumps WP Hooks (Filters and Actions) | |
* in the order of execution in Markdown format. | |
* | |
* File hooks.md in the root location of the WP setup. | |
* | |
* @param bool $actions - render actions | |
* @param bool $filters - render filters | |
* @param bool $unique - render unique hooks | |
*/ | |
function dump_hooks($actions = true, $filters = true, $unique = true) | |
{ | |
global $__hash; | |
$__hash = []; | |
$fp = fopen( ABSPATH . 'hooks.md', 'w'); | |
fwrite($fp, "|#|HOOK|TYPE|METHOD|FILE|\n"); | |
fwrite($fp, "|---|---|---|---|---|\n"); | |
fclose($fp); | |
add_action( 'all', function($hook) use ($actions, $filters, $unique) { | |
global $__hash; | |
if ($unique && in_array($hook, $__hash)) | |
return; | |
$backtrace = debug_backtrace(false, 5); | |
$caller = $backtrace[4]; | |
$type = stristr($backtrace[3]['function'], 'filters') ? 'FILTER' : 'ACTION'; | |
if ($type === 'FILTER' && $filters === false) | |
return; | |
if ($type === 'ACTION' && $actions === false) | |
return; | |
$__hash[] = $hook; | |
$method = $caller['function'] ?? 'N/A'; | |
$file = isset($caller['file']) ? str_replace(ABSPATH, '', $caller['file']) : false; | |
$line = $caller['line'] ?? false; | |
$ref = ($file && $line) ? "$file($line)" : 'N/A'; | |
$n = count($__hash); | |
$line = "|$n|$hook|$type|$method|$ref|\n"; | |
$fp = fopen( ABSPATH . 'hooks.md', 'a+'); | |
fwrite($fp, $line); | |
fclose($fp); | |
}); | |
} | |
dump_hooks(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment