Every clean customisation of WordPress goes through hooks. Rather than editing core, theme or plugin files, which would be overwritten at the next update, you attach your own function to a precise point in the execution. WordPress will call it at the right moment. This is how every plugin works, WooCommerce included.
Actions and filters: the only difference to remember
An action lets you do something at a given moment: send an email after an order, add a script to the header, register a content type. It returns nothing. A filter lets you change a value before it is used: a title, a price, the content of a post. It receives the value and must always return it, changed or not.
Using an action
add_action takes the hook name and the function to run. Here, a verification tag is added to the header of every page:
add_action( 'wp_head', function () {
echo '<meta name="verification" content="abc123">';
} );
Using a filter
add_filter works the same way, but the function receives a value and returns it. Here, post excerpts are shortened to 30 words:
add_filter( 'excerpt_length', function ( $length ) {
return 30;
} );
The most common mistake with a filter is forgetting the return. The value then becomes empty, and the title, price or content disappears from the site.
Priority and number of arguments
The third parameter sets the priority: 10 by default, and the lower the number, the earlier the function runs. The fourth tells how many arguments your function receives. Many hooks pass several, but WordPress only passes one unless you say otherwise.
add_filter( 'the_title', function ( $title, $post_id ) {
if ( get_post_type( $post_id ) === 'product' ) {
$title = $title . ' (WooCommerce)';
}
return $title;
}, 20, 2 );
Creating your own hooks
In a custom theme or plugin, add your own hook points with do_action and apply_filters. Another developer can then change the behaviour without touching your code, and your updates will not break their additions.
do_action( 'myplugin_after_send', $order );
$total = apply_filters( 'myplugin_total', $total, $order );
Removing a hook added by a plugin
remove_action and remove_filter only work if you give exactly the same function and the same priority as the original addition, and if you call them after it. That is why they are often placed in a later hook, such as init or wp_loaded. An anonymous function cannot be removed at all: a good reason to name the functions in your own plugins.
Frequently asked questions
Where should this code go?
In a child theme's functions.php, or better in a small dedicated plugin or a mu-plugin: your code then survives a theme change.
How can I see which hooks run on a page?
The Query Monitor plugin lists the hooks fired on the page and the functions attached to each one, with their priority. It is the fastest tool to understand why a behaviour changes.
Author
Walid SadfiWalid Sadfi is the founder of Evolurise, a team of engineers deployed on site with its clients: custom development (WordPress, Drupal, React, Laravel), AI agents, DevOps and team training. With 8 years of experience, he supports SMEs and large companies, from understanding the problem to deploying the solution. On this blog he shares practical, field-tested insights.