Woo Product Table Hooks: Actions and filters

Estimated reading: 2 minutes 166 views

Introduction: What are hooks?

Hooks in WordPress essentially allow you to change or add code without editing core files. They are used extensively throughout WordPress and WooCommerce and are very useful for developers.

There are two types of hook: actions and filters.

  • Action Hooks allow you to insert custom code at various points (wherever the hook is run).
  • Filter Hooks allow you to manipulate and return a variable which it passes (for instance a product price).

Using hooks

There are variety of ways to add your custom code to manipulate code by hooks:

Using action hooks

To execute your own code, you hook in by using the action hook do_action('action_name');. Here is where to place your code:

Using filter hooks

Filter hooks are called throughout the code using apply_filter( 'filter_name', $variable );.To manipulate the passed variable, you can do something like the following:

With filters, you must return a value.

To learn more about options for using hooks and filters, see the Snippet doc section.

Example

In the example below, both action hooks and filter hooks are used. This tutorial teaches you how to change and add fields to the checkout.

  • Actions are used to:
    • Add a new field to the checkout
    • Add that new field to the order.
  • Filters are used to:
    • Override the labels and placeholders of existing fields.
    • Make an existing field optional while it used to be required.
    • Remove existing fields.

Available action hooks and filters

For details on what the action hooks and filters do, refer to the Woo Product Table Hooks Reference.

CONTENTS