WordPress

Hook

Hook is WordPress's name for a point in the code where other code can attach. Core, themes, and plugins all use hooks to add or change behavior without editing each other's files.

How it is measured

Hooks come in two forms, actions and filters, both registered with a name, a callback, a priority, and an accepted argument count. WordPress keeps them in the global $wp_filter array. A hook fires when core calls do_action( 'name' ) or apply_filters( 'name', $value ).

You can enumerate them for a request with Query Monitor's Hooks panel, or in code with has_action(), has_filter(), and did_action(). The developer reference lists the documented core hooks by name and version added.

Worked example

A WooCommerce store has a plugin that adds a handling fee and another that removes it for orders over 100 dollars. Both attach to woocommerce_cart_calculate_fees, one at priority 10 and one at 20. The remover runs second, so the fee vanishes for large carts.

A theme update changes the second callback to priority 5. Now the remover runs before the fee is added and customers are charged 4.50 on every cart. Support tickets reach 30 in a day before anyone checks the priorities.

How it differs

A hook is the attachment point, and an action is a hook type that runs code without taking over a value. A filter is the other type, passing a value through. Saying hook when you mean the specific type is fine in conversation but ambiguous in a bug report.

Common errors

Mixing up the priority number direction, since lower runs earlier. Removing a callback with remove_action before it was added. Trying to remove a class method callback without the original object instance. Using a core hook name from memory that was deprecated. Hooking too early and finding that functions you need are not yet loaded.

In practice

When a plugin behaves oddly, open Query Monitor and find the hook it uses, then check the other callbacks on that name and their priorities. Prefix your own custom hook names with your plugin slug so another developer can search for them.

See also

Action, Filter

Sources

Count this on a real site.

Watch my website