DEV Community

Cover image for How to Get a List of Hooks in a WordPress Plugin
Muhammad Medhat
Muhammad Medhat

Posted on

How to Get a List of Hooks in a WordPress Plugin

Introduction

In WordPress, hooks are used to modify or extend the functionality of themes and plugins. They are divided into two types:

  • Actions: Allow executing custom code at specific points (e.g., init, wp_footer).
  • Filters: Modify data before it is displayed (e.g., the_content, wp_title).

If you're working with a plugin and need to find its hooks, here are several methods to list them.

1. Manually Searching the Plugin Files

One of the simplest ways is searching for add_action() and add_filter() inside the plugin’s files.

Using Terminal (Linux/macOS)

grep -r "add_action" /path/to/plugin/
grep -r "add_filter" /path/to/plugin/
Enter fullscreen mode Exit fullscreen mode

Using Command Prompt (Windows)

findstr /S /I "add_action" "C:\path\to\plugin\*.*"
findstr /S /I "add_filter" "C:\path\to\plugin\*.*"
Enter fullscreen mode Exit fullscreen mode

Using a Code Editor

  • VS Code: Press Ctrl + Shift + F and search add_action or add_filter.
  • Notepad++: Use Find in Files (Ctrl + Shift + F).

2. Using a Debugging Plugin

Plugins like Query Monitor can help list active hooks.

Steps to Use Query Monitor

  1. Install and activate Query Monitor.
  2. Open your website and inspect the Query Monitor panel.
  3. Navigate to the Hooks & Actions section to see executed hooks.

3. Logging Hooks with Custom Code

To capture hooks in real-time, you can log them using:

function log_all_hooks($hook) {
    error_log($hook);  // Save to debug.log
}
add_action('all', 'log_all_hooks');
Enter fullscreen mode Exit fullscreen mode

🔹 View logs in: wp-content/debug.log

tail -f wp-content/debug.log
Enter fullscreen mode Exit fullscreen mode

4. Using a Custom WP-CLI Command

WP-CLI does not provide a built-in way to list hooks, but you can create a custom command:

Adding a Custom WP-CLI Command

Add this code to functions.php or a custom plugin:

if (defined('WP_CLI') && WP_CLI) {
    WP_CLI::add_command('hooks list', function() {
        global $wp_filter;
        foreach ($wp_filter as $hook => $callbacks) {
            WP_CLI::log($hook);
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

Running the Command

wp hooks list
Enter fullscreen mode Exit fullscreen mode

This will output all registered hooks in WordPress.

To get it for a specific plugin

if (defined('WP_CLI') && WP_CLI) {
    WP_CLI::add_command('hooks list', function($args) {
        global $wp_filter;
        $plugin_name = isset($args[0]) ? $args[0] : '';
        foreach ($wp_filter as $hook => $callbacks) {
            if (!$plugin_name || strpos($hook, $plugin_name) !== false) {
                WP_CLI::log($hook);
            }
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

Running the Command

wp hooks list woocommerce  # Replace 'woocommerce' with your plugin
Enter fullscreen mode Exit fullscreen mode

This will output all registered hooks related to the specified plugin.

Conclusion

Finding hooks in a plugin is essential for customizing or debugging WordPress functionality. Use:

  • Manual search for quick inspection.
  • Debugging tools for real-time monitoring.
  • Custom logging for deeper analysis.
  • WP-CLI for efficient listing.

Top comments (0)