WooCommerce: View All The Hooks That Are Fired On A Page

Actions and Filters are key to the functionality of WordPress and WooCommerce but it can can sometimes be difficult to work out what filters and actions have fired on a page.

In this article we’re going to take a look at how to list the filters and actions that get fired on a page, we’ll also look at how to list the functions that are hooked into them.

Code to List the Filters that are Fired on a Page

There are a lot of snippets knocking around that will list the actions and filters on a page and there are often subtle differences in the functionality they provide. For this article I’m going to use a slightly modified version of a snippet that was written by WordPress genius Fuxia Scholz, because –

  • It’s great
  • It’s does a lot in a few lines of code
  • It lists filters/actions and the functions that are hooked to them
  • It displays the filters and actions in a nice formatted list, the name of each filter/action is also a link that points to a list of the functions attached to the hook

Here’s the code, I’ll go though it in more detail underneath the code snippet

function list_comment_filters()
    global $wp_filter;

    $comment_filters = array();
    $h1 = '<h1>Current Comment Filters</h1>';
    $out = '';
    $toc = '<ul>';

    foreach ($wp_filter as $key => $val) {
        if (FALSE !== strpos($key, 'comment')) {
            $comment_filters[$key] = var_export($val, TRUE);

    foreach ($comment_filters as $name => $arr_vals) {
        $out .= "<h2 id=$name>$name</h2><pre>" . $arr_vals . '</pre>';
        $toc .= "<li><a href='#$name'>$name</a></li>";

    echo "$h1$toc</ul>$out";

add_action('wp_footer', 'list_comment_filters');

The code begins by attaching a function named “list_comment_filters” to the “wp_footer” action.

It then defines the “list_comment_filters” function.

The function begins by getting a reference to the $wp_filter global object, this is the object that WordPress uses to store details of all of the actions and filters that have been registered during the execution of the current page code.

The function then sets up a few variables

  • $comment_filters an array that will hold the filter/action data
  • $h1 a string that will hold the header for the filter/action data. It’s worth pointing out at this point that the code only gets filters and actions that are linked to the comment functionality in WordPress, but it would only take a few modifications to get the code to report on other actions and filters. We’ll take a look at how to do that shortly.
  • The code then sets up an other couple of strings $out and $toc which it will use to store the hook data that it outputs to the screen.

Having set up the variables the code embarks on a foreach loop that loops over the values in the $wp_filter global object, the loop uses the following key value pair

  • $key which holds the name of the filter or action being fired, e.g. “the_content”
  • $val this is an instance of the WP_Hook class that holds details of the hooks linked to the action or filter

The first line of the foreach is an if statement that checks if the hook name (held in the $key variable) contains the text “comment”, this is so the code only outputs the details of hooks that are related to comments. The value could be changed to something else if you wanted to output the details of different hooks, or you could remove the if check completely if you wanted to list all hooks in the $wp_filter.

Inside the if  statement the code uses the following line to add the details of any relevant hooks to the $comment_filters array

$comment_filters[$key] = var_export($val, TRUE);

This one line of code adds a key value pair to $comment_filters array that has the hook name as the key and a formatted string created by PHP’s var_export function as the value. The code handles a case where the $wp_filter object contains multiple values for the same key.

Having found the relevant hooks the code then loops over the values in the $comment_filters array and  adds string values to the $out and $toc variables that contain the details of the current hook.

Finally the code uses string interpolation to echo the values of the $h1, $toc and $out variables to the screen.

If the code is added to a WooCommerce or WordPress site then then it displays a list of the functions it finds towards the bottom of the screen

an image showing how the code lists the title of each hook it finds

As mentioned before, you can click the title of a hook and you’ll be taken to a summary of the functions assigned to it

the codes output of the functions linked to the comment_url hook

The code will list both actions and filters that match criteria set in the if  statement.

Code to List the Functions that are Fired by a Specific Action or Filter

It would be possible to change the if statement in the code so it only returned the details of one hook, but the function can be simplified to only return the details of one hook. Here’s a simplified function that you could use

function hwn_list_functions_for_hook()
    global $wp_filter;

    $filter_name = 'comment_url';
    $h1 = '<h2>Function data for ' . $filter_name . ' hook</h2>';

    $out = "<pre>" . var_export($wp_filter[$filter_name], TRUE) . '</pre>';

    echo "$h1$out";

add_action('wp_footer', 'hwn_list_functions_for_hook');

Final Thoughts

Hopefully you can use the two snippets in this article to find out what is hooked to a set of functions or actions when you are debugging your code. There are a few other things I wanted to cover regarding this that I will look at in a couple of follow up articles

I’ll also include further reading list to point you towards a few other resources that should be helpful if you’re looking into this.

As always, please don’t hesitate to let me know if you have any questions or queries in the comments.

Further Reading

https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/ – The way that WordPress stores the hook information was changed after version 4.7, this article documents the changes made.
https://www.smashingmagazine.com/2012/02/inside-wordpress-actions-filters/ – An excellent and thorough article about hooks and filters in Smashing magazine
https://wordpress.stackexchange.com/questions/17394/how-to-know-what-functions-are-hooked-to-an-action-filter?noredirect=1&lq=1 – The WordPress Stack Exchange post that contains the code that this article is based around
https://adambrown.info/p/wp_hooks – Adam R. Brown’s full list of all the hooks in WordPress, the site lists hooks from previous version and lists when hooks were added and removed from the WordPress core

Finally, the following two articles on this site provide an introduction to how actions and filters work with WooCommerce

do_action and add_action in Woocommerce and WordPress
How to use add_filter and apply_filters in WooCommerce


Leave a Comment