WordPress Hooks and Filters Explained with Examples (2026 Guide)

Understand WordPress actions and filters in 2026 with clear examples: add_action, add_filter, priority, arguments, removing hooks and creating your own.

WordPress hooks and filters explained with code examples, 2026 guide

Table of Contents

WordPress hooks let you change how WordPress works without editing core files. There are two kinds: actions, which run your code at a certain moment (add_action), and filters, which receive a value, change it and return it (add_filter). Put your hooks in a child theme’s functions.php, a code snippets plugin or your own small plugin, never in WordPress core.

Key takeaways
  • Actions do something. Filters change something and must return it.
  • Priority decides the order: lower numbers run first, the default is 10.
  • The accepted arguments number must match how many values you use.
  • Your own plugins and themes can create hooks with do_action and apply_filters.
  • Hooks are why WordPress sites can be customised and still update safely.

What are hooks in WordPress, in plain English?

Picture WordPress as a train that stops at many stations while it builds a page: loading plugins, setting up the theme, printing the head, printing the content, printing the footer. Hooks are those stations. You can tell WordPress, “When you reach the footer station, also run my code” (an action), or “Before you show the post title, hand it to me so I can change it” (a filter).

Every serious plugin, including WooCommerce, Yoast and Elementor, is built on hooks. Learning them is the moment WordPress stops being a black box.

How do you use an action hook?

An action runs your function at a set point. This example adds a small notice to every page footer:

add_action( 'wp_footer', 'twx_footer_notice' );
function twx_footer_notice() {
    echo '<p class="site-note">Free shipping on orders over $50</p>';
}

Common actions include init (register post types), wp_enqueue_scripts (load CSS and JS), wp_head (add tags to the head), save_post (run code when a post is saved) and woocommerce_thankyou (run code after an order).

How do you use a filter hook?

A filter receives a value and must return it, changed or not. This example shortens excerpts to 25 words:

add_filter( 'excerpt_length', 'twx_excerpt_length', 20 );
function twx_excerpt_length( $length ) {
    return 25;
}

Forgetting to return the value is the most common filter bug. The result is empty titles, missing content or a blank page. Useful filters include the_content, the_title, body_class, upload_mimes and many WooCommerce filters for prices and checkout fields.

What do priority and accepted arguments mean?

ParameterDefaultWhat it does
Hook nameRequiredThe station you attach to, for example wp_footer
CallbackRequiredYour function name or an anonymous function
Priority10Order of execution. 5 runs before 10, 99 runs late
Accepted args1How many values your function receives

For example, add_action( 'save_post', 'my_fn', 10, 3 ); lets your function receive the post ID, the post object and whether it is an update.

Custom developmentNeed a custom feature, plugin or theme?

Custom post types, plugins, theme tweaks and integrations written the WordPress way, update-safe and documented.

Discuss your project

How do you remove a hook added by a plugin or theme?

Use remove_action or remove_filter with the same hook name, the same callback and the same priority. Run it after the original was added, for example inside an init or wp action. If the original callback is a method on a class object, you need access to that object, which plugins often expose through a global or a function.

How do you create your own hooks?

When you build a theme or plugin, add hooks so others (or future you) can extend it:

do_action( 'twx_before_pricing_table', $plan );
$price = apply_filters( 'twx_plan_price', $price, $plan );

Prefix hook names with your project name to avoid clashes with other plugins.

What are the best practices for hooks?

  • Keep custom code in a child theme, a snippets plugin or a site plugin, so theme updates do not erase it.
  • Prefix every function name to avoid fatal “cannot redeclare” errors.
  • Load scripts with wp_enqueue_scripts, not by printing script tags in the head.
  • Check is_admin(), the current page or the post type so code only runs where needed.
  • Use the Query Monitor plugin to see which hooks fire on a page.

Ready for the next step? Learn to build a WordPress plugin from scratch or create custom post types.

Frequently asked questions

What is the difference between an action and a filter in WordPress?

An action runs code at a certain moment and returns nothing. A filter receives a value, can change it, and must return it.

Where should I put custom hook code?

In a child theme functions.php, a code snippets plugin or a small custom plugin. Never in WordPress core or a parent theme that updates.

What does hook priority mean?

It sets the order in which functions on the same hook run. Lower numbers run first and the default is 10.

Why did my filter make content disappear?

The function probably did not return the value. Every filter callback must return something, usually the modified input.

How do I find which hooks a plugin uses?

Read its documentation, search its code for do_action and apply_filters, or use the Query Monitor plugin to see hooks on a page.

Sources

Custom developmentNeed a custom feature, plugin or theme?

Custom post types, plugins, theme tweaks and integrations written the WordPress way, update-safe and documented.

Discuss your project
Written by Tanvir H

WordPress and Elementor web designer at Tanvir WebXprt, Bangladesh, with 7+ years of experience and 650+ projects for clients worldwide. Tanvir builds, fixes and speeds up WordPress sites every week and writes these guides from that hands-on work.

Keep learning

More WordPress guides

Done for you

Rather have an expert run your WordPress site?

Tanvir H is a Bangladesh-based WordPress and Elementor web designer with 7+ years of experience, 650+ projects and a 4.9 client rating. New websites, redesigns, fixes, speed, security and SEO, handled by one expert who explains everything in plain English and works with clients worldwide.

Website developmentWordPress, Elementor & WooCommerce sitesfrom $899
Fixes & speedErrors, hacks, Core Web Vitals, upgradesFree quote
SEO & paid adsRank on Google, run Meta & Google Adsfrom $300
Creative designWeb, graphic & brand identityfrom $220

Contents

Was this helpful? Spread the word and share with your network!
Facebook
Pinterest
LinkedIn
Reddit
VK
X
Telegram
Threads
Tumblr
Email
WhatsApp
Pocket

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Article