← All posts

WordPress hooks: actions and filters, a practical guide

Hooks let you change WordPress without touching core or the parent theme. Here is how to use them well, and the traps we run into on real projects.

WordPress hooks: actions and filters, a practical guide

Key points

  • An action runs code at a given moment; a filter changes a value and returns it.
  • Priority and argument count prevent most bugs.
  • Well-named hooks in your own code make your theme extensible.

When a client asks us to “tweak one little thing” in a plugin, the temptation is to edit its files directly. That is the surest way to lose the change at the next update. Hooks exist precisely to avoid this.

Action vs filter: the real difference

An action signals that something is happening: a post is saved, the page head is printed, a user logs in. You attach code that does something and returns nothing.

A filter passes a value through your function: a post title, an email body, a list of CSS classes. You must always return the value, changed or not. A missing return is the most common bug we fix on existing sites: content silently disappears.

// Action: add a tag to the <head>
add_action('wp_head', function () {
    echo '<meta name="theme-color" content="#0b0d12">';
});

// Filter: shorter excerpts
add_filter('excerpt_length', fn() => 24);

Priority and arguments

The third parameter is the priority (10 by default): lower runs earlier. If your filter seems ignored, another plugin is often running after it with a higher number.

The fourth parameter is how many arguments your function receives. The default is one. For save_post, which passes three, you must say so:

add_action('save_post', 'wts_on_save', 10, 3);
function wts_on_save($post_id, $post, $update) {
    if (wp_is_post_revision($post_id) || defined('DOING_AUTOSAVE')) return;
    // ...
}

Traps worth knowing

  • Infinite loops: calling wp_update_post() inside save_post fires the hook again. Remove it with remove_action() first, then add it back.
  • Anonymous functions cannot be removed by a child theme. In a plugin meant for others, use named functions.
  • Wrong timing: registering a post type outside init, or enqueuing scripts outside wp_enqueue_scripts, leads to random behaviour.

Adding your own hooks

In our themes we place do_action() and apply_filters() at key points: before and after content, in post cards, in menus. Clients or other developers can add a block without editing the theme. Always prefix your hooks (for example wts_) to avoid collisions.

FAQ

How do I see which hooks run on a page?

The Query Monitor plugin lists fired hooks and the functions attached to them. It is the first tool we install on a staging site.

Where should this code live?

In a child theme’s functions.php or, better, in a small dedicated plugin, so it survives a theme change.

Need to adapt a plugin without breaking updates? That is the core of our custom WordPress development work.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →