WooCommerce. How to find functions linked to specific hooks in WordPress and WooCommerce

In WordPress and WooCommerce, hooks are an important part of how to customise and extend the functionality of a WordPress theme. Hooks allow developers to add functionality in specific places in the codebase without changing the original files.

But what if you want to find the functions that are activated on a given hook?

What is a Hook?
A hook in WordPress and WooCommerce can be either an action hook or a filter hook:

  • Action Hooks: Used to perform functions at specific locations in the code.
  • Filter Hooks: Used to change data before it is passed on (e.g. change the content of a post or product description).

For example, in WooCommerce there are many predefined hooks that you can use to add extra content before or after a product, on product pages, and much more.

How to find functions associated with a specific hook

If you want to identify which functions are associated with a particular hook, you can debug it using the following method. Insert the code below in your header.php example and remove it again when you are done debugging. Note that this debugging method is not hidden and will be visible to all site visitors.

In this example, we look at the hook woocommerce_before_single_product_summary.

<?php
global $wp_filter;
echo '
';
print_r($wp_filter['woocommerce_before_single_product_summary']);
echo '
'; ?>

Remember to use this method temporarily or on a development or staging environment.

What do I use it for?
When I'm working on fixing code in themes developed by others, it can sometimes be necessary to disable certain hooks set by the active theme itself and/or other plugins. This typically happens if we have developed new features that replace existing ones. For example, it could be a product gallery. In such cases, it's important to get an overview of which hooks are being executed so that I can remove the relevant hooks without affecting the rest of the functionality.