Skip to content

Instantly share code, notes, and snippets.

@maxchirkov
Last active November 26, 2018 14:37
Show Gist options
  • Save maxchirkov/b3391f4d23e9c219b66d1a1ba9699805 to your computer and use it in GitHub Desktop.
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.
/**
* 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