Skip to content

Instantly share code, notes, and snippets.

@ve3
Last active December 24, 2023 13:33
Show Gist options
  • Save ve3/96c50c671e06dde475f6e3c80492f7e5 to your computer and use it in GitHub Desktop.
Save ve3/96c50c671e06dde475f6e3c80492f7e5 to your computer and use it in GitHub Desktop.
WordPress hooks (actions & filters) trace.
<?php
/**
* Plugin Name: WP hooks trace
* Description: Enable trace > browse or work > disable trace. And everything will be written into this plugin folder /hooks-trace.txt file.
* License: MIT
*/
// You can use this plugin for any purpose without attribution required.
add_filter('plugin_row_meta', 'rdht_pluginRowMeta', 10, 4);
function rdht_pluginRowMeta(array $plugin_meta, string $plugin_file, array $plugin_data, string $status)
{
$thisFileName = plugin_basename(__FILE__);
if ($plugin_file === $thisFileName) {
$traceHooksOptions = get_transient('rdht_tracehooks');
if ($traceHooksOptions === '1') {
// if marked as trace hooks.
$plugin_meta[] = '<a href="?rdht_tracehooks=false">Disable trace hooks</a>';
} else {
$plugin_meta[] = '<a href="?rdht_tracehooks=true">Enable trace hooks</a>';
}
unset($traceHooksOptions);
}
unset($thisFileName);
return $plugin_meta;
}// rdht_pluginRowMeta
add_action('init', 'rdht_updateTraceOption');
function rdht_updateTraceOption(string $plugin)
{
if (isset($_REQUEST['rdht_tracehooks']) && $_REQUEST['rdht_tracehooks'] === 'true') {
set_transient('rdht_tracehooks', '1', time() + (2 * DAY_IN_SECONDS));
} elseif (isset($_REQUEST['rdht_tracehooks']) && $_REQUEST['rdht_tracehooks'] === 'false') {
delete_transient('rdht_tracehooks');
delete_transient('rdht_tracehooks_started');
}
if (isset($_REQUEST['rdht_tracehooks'])) {
$url = remove_query_arg('rdht_tracehooks');
wp_redirect($url);
exit();
}
}
add_action('shutdown', 'rdht_traceHooks');
function rdht_traceHooks()
{
$traceHooksOptions = get_transient('rdht_tracehooks');
if ($traceHooksOptions !== '1') {
return ;
}
unset($traceHooksOptions);
if (stripos($_SERVER['REQUEST_URI'], 'plugins.php') !== false && stripos($_SERVER['REQUEST_URI'], 'rdht_tracehooks=') !== false) {
return ;
}
$logFile = __DIR__ . DIRECTORY_SEPARATOR . 'hooks-trace.txt';
$isStarted = get_transient('rdht_tracehooks_started');
if ($isStarted !== '1') {
if (is_file($logFile)) {
unlink($logFile);
}
}
unset($isStarted);
$contents = '';
global $wp_actions;
$contents .= 'page: ' . ($_SERVER['REQUEST_URI'] ?? 'UNKNOWN') . ' method: ' . ($_SERVER['REQUEST_METHOD'] ?? 'GET') . "\n";
$contents .= '#ACTIONS (hits) =============================' . "\n";
if (is_iterable($wp_actions)) {
foreach ($wp_actions as $name => $hits) {
$contents .= $name . ' (' . $hits . ')' . "\n";
}
unset($hits, $name);
}
$contents .= 'End actions. ================================' . "\n";
$contents .= "\n";
global $wp_filters;
$contents .= '#FILTERS ====================================' . "\n";
if (is_iterable($wp_filters)) {
foreach ($wp_filters as $name => $items) {
$contents .= $name . "\n";
}
unset($items, $name);
}
$contents .= 'End filters. ================================' . "\n";
$contents .= "\n";
// end of all record (new line);
$contents .= "\n";
file_put_contents($logFile, $contents, FILE_APPEND);
set_transient('rdht_tracehooks_started', '1', time() + (2 * DAY_IN_SECONDS));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment