Currently reading: Black Like Me by John Howard Griffin

Get a list of all WordPress hooks that have run

In order to truly catch them all, we can use a "must-use" plugin. These live in the /wp-content/mu-plugins folder as flat PHP files that are automatically included, in alphabetical order, before much has happened in the WordPress core loading process.

Normal plugins load fairly early, but these are pulled in even earlier. Their inclusion can't be prevented by the relevant back-end admin page either ⸺ they simply don't show up in your plugin's list. By the time that thing's ready to go, these "special" plugins have been loaded for ages.

For our little hook-gathering venture, drop this code in one such plugin. Call it all-hooks.php or something so it's at the top of the list of alphabetically-included files.

add_action('all', function ($name) {
    static $hooks = [];

    if (did_action($name)) {
        $hooks[] = $name;
    }
    
    if ('shutdown' === $name) {
        print_r($hooks);
    }
});

While digging through source code is the "correct" way to sniff out what hooks are running so you might tap into one, dropping a long list of them at the bottom of your site is certainly an option.


If you've managed not to see a static variable before, consider this your simple crash course.

Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

Therefore, $hooks only gets instantiated as an empty array on the first pass; it retains its value as the as PHP execution continues and the function is repeatedly called over its lifetime.

The difference between a static variable and a global one is the access scope of the former remains the same, limited to its parent, while the latter is available everywhere.

Leave a comment

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