WordPress Hooks Are the Whole Game
If you want to change how WordPress behaves — add a notice to every post, redirect users after login, register a custom post type, tweak what a query returns — there is exactly one sanctioned way to do it: WordPress hooks. You never edit core files. You never hack a parent theme. You hook your code into named points in WordPress’s execution flow, and WordPress runs it for you. Every plugin you have ever installed, from WooCommerce to Rank Math, is built on this one mechanism.
Hooks sound abstract until you write your first one, and then they click permanently. This guide is the one I wish existed when I was learning: the two types of hooks, the exact rules that govern them, the hooks you will use every week, how to build your own, and — the part most tutorials skip — how to debug a hook that refuses to fire.
What Are WordPress Hooks?
A hook is a named point in WordPress’s execution where your code can plug in. WordPress core, plugins, and themes place these named points all over the codebase. You register a callback function against a hook name, and when WordPress reaches that point, it calls your function.
There are exactly two kinds:
- Actions — “this is happening now.” Your function does something: prints markup, sends an email, registers a post type. It returns nothing.
- Filters — “here is a value, change it if you want.” Your function receives a value, modifies it, and must return it.
Under the hood they are the same mechanism — add_action() is technically a wrapper around add_filter() in WordPress core — but treat them as different in your head, because the one rule that matters is different: a filter that forgets to return its value silently breaks whatever it was filtering.
Actions vs Filters at a Glance
| Action | Filter | |
|---|---|---|
| Purpose | Do something at a given moment | Change a value before it is used |
| Receives | Optional context arguments | A value, always as the first argument |
| Returns | Nothing | Must return a value |
| Attach with | add_action() |
add_filter() |
| Core fires with | do_action() |
apply_filters() |
| Typical use | Enqueue assets, register post types, send mail | Excerpt length, post titles, query arguments |
Your First Action Hook
An action runs your function at a set point. The simplest useful example: add a small notice to the footer of every page.
function htsi_footer_notice() {
echo '<p>Built with WordPress — and a little patience.</p>';
}
add_action( 'wp_footer', 'htsi_footer_notice' );
The signature you need to memorise:
add_action( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 );
Only the first two arguments are required. The other two — priority and accepted arguments — are where most hook bugs live, and they get their own section below.
A more realistic action is registering a custom post type. Post types must be registered on init, after WordPress has loaded but before anything tries to use them:
function htsi_register_book_post_type() {
register_post_type( 'book', array(
'labels' => array(
'name' => 'Books',
'singular_name' => 'Book',
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
) );
}
add_action( 'init', 'htsi_register_book_post_type' );
Hooking this to the wrong moment — say, wp_loaded is too late for some things, and running it at the top level of your plugin file is too early, before WordPress knows what a post type is — is the single most common reason a hook “doesn’t work”. Timing is everything, and the hook reference tells you when each one fires.
Your First Filter Hook
A filter receives a value and must return it, changed or not. This example shortens excerpts to 25 words:
function htsi_excerpt_length( $length ) {
return 25;
}
add_filter( 'excerpt_length', 'htsi_excerpt_length' );
And this one appends a reading-time note to every post:
function htsi_add_reading_time( $content ) {
if ( ! is_single() ) {
return $content;
}
$words = str_word_count( wp_strip_all_tags( $content ) );
$minutes = max( 1, ceil( $words / 200 ) );
return $content . '<p>Reading time: ' . $minutes . ' min.</p>';
}
add_filter( 'the_content', 'htsi_add_reading_time' );
Notice the guard clause: the filter returns $content untouched when the condition doesn’t apply. That is the discipline filters demand. Forgetting the return is the number one hook bug in existence — WordPress receives null instead of the content, and your post body silently vanishes from the page. If a page ever goes blank after you add a filter, check the return statement first.
WordPress Hook Priority and Accepted Args: The Two Numbers That Decide Everything
Two optional parameters control how your callback behaves, and misunderstanding either one produces bugs that look like WordPress is broken.
Priority: who runs first
Priority is a positive integer, default 10. Lower numbers run first; higher numbers run later. If two callbacks share the same priority, they run in the order they were registered. The official handbook puts it plainly: a function with priority 9 runs before one with priority 10, and one with priority 11 runs after.
add_action( 'init', 'htsi_run_me_early', 9 );
add_action( 'init', 'htsi_run_me_normal' ); // priority 10
add_action( 'init', 'htsi_run_me_late', 11 );
Use priority deliberately, not superstitiously. The classic case: your theme enqueues a stylesheet at priority 10, and you want your override to load after it, so you enqueue yours at priority 20. The other classic case: a plugin’s filter mangles a value and you need yours to run after it, so you pick a higher number. Don’t use 999 “just to be safe” — it makes your code unreadable and hides ordering assumptions that will bite the next developer.
Accepted args: how many values your callback gets
By default your callback receives exactly one argument, even if the hook passes more. If you need the rest, you must say so with the fourth parameter — and this is the trap. save_post, the hook that fires after a post is saved, passes three values: the post ID, the post object, and a boolean telling you whether this is an update or a brand-new post:
add_action( 'save_post', 'htsi_notify_on_new_post', 10, 3 );
function htsi_notify_on_new_post( $post_id, $post, $update ) {
if ( $update ) {
return; // Only act on brand-new posts.
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Send the notification here.
}
Leave off the 3 and your callback receives only $post_id — the other two parameters arrive as nothing, and PHP throws an argument-count error if your function declares them. The reverse mistake is just as common: declaring three parameters while registering the default of one. Count the arguments the hook passes, then count the number you registered. They must match.
Two save_post gotchas worth knowing: it fires for every post type including revisions, so check $post->post_type; and if your callback calls wp_update_post(), it triggers save_post again — an infinite loop unless you unhook first or guard with a flag. For most “after a post is fully saved, terms and meta included” work, the newer wp_after_insert_post action (WordPress 5.6+) is the better hook: it passes the post, the update flag, and the pre-update post object.
Where to Put Your Hook Code
Hooks belong in one of three places, and the choice matters:
- A child theme’s
functions.php— for anything presentation-related: excerpt lengths, body classes, template tweaks. Never in a parent theme that updates, and never in core. - Your own small plugin — for functionality that should survive a theme change: custom post types, notifications, integrations. A single file with a plugin header is enough.
- A must-use plugin (
wp-content/mu-plugins/) — for code that must always run and cannot be accidentally deactivated, like a site-wide security tweak. Mu-plugins load before normal plugins and can’t be disabled from the admin.
For quick experiments, the Code Snippets plugin is a fine middle ground — it stores snippets in the database and lets you toggle them without touching files. Whatever you choose, prefix every callback with something unique to you (htsi_ in this guide’s examples) so your function names never collide with a plugin’s.
Removing Hooks: The Exact-Match Rule
You can remove a callback another theme or plugin registered, but removal is an exact match: same hook name, same callback, and the same priority it was registered with. And it must run after the original registration — removing a hook before it exists does nothing, silently.
// The parent theme registered this at priority 10:
add_action( 'wp_footer', 'parent_theme_credit', 10 );
// Your child theme removes it — same hook, callback, and priority:
remove_action( 'wp_footer', 'parent_theme_credit', 10 );
Two hard limits: remove_action() and remove_filter() return false silently when nothing matches, so a typo fails without a word; and closures cannot be removed — if a plugin registered its callback as an anonymous function, there is no name to reference, so you cannot unhook it at all. That is a genuine reason to prefer named functions in code you expect others to extend.
When removal timing is the problem, hook your removal to a later action than the registration. A common pattern is removing a parent theme’s hook inside a callback on after_setup_theme, which fires after the parent theme’s functions.php has loaded.
The Hooks Every WordPress Developer Should Know by Heart
You don’t need to memorise hundreds of hooks. These are the ones that cover most real work:
| Hook | Type | When it runs / what it filters |
|---|---|---|
init |
Action | After WordPress loads; register post types and taxonomies here |
wp_enqueue_scripts |
Action | The right place to enqueue front-end CSS and JS |
wp_head |
Action | Inside <head>; add meta tags and tracking snippets |
wp_body_open |
Action | Right after the opening <body> tag |
wp_footer |
Action | Before </body>; footer scripts and notices |
save_post |
Action | After a post is saved; receives ID, post object, update flag |
save_post_{$post_type} |
Action | Same, but only for one post type — e.g. save_post_book |
pre_get_posts |
Action | Modify the main query before it runs — the correct way to change archives |
template_redirect |
Action | After the query, before the template; redirects live here |
admin_init |
Action | Admin-side setup; register settings here |
wp_login |
Action | After a successful login; receives username and user object |
the_content |
Filter | Post content before display |
the_title |
Filter | Post title before display |
excerpt_length |
Filter | Number of words in the excerpt |
body_class |
Filter | CSS classes on the <body> tag |
upload_mimes |
Filter | Which file types WordPress allows in uploads |
Two of these deserve a warning. pre_get_posts is the correct way to change what an archive shows — but it fires for every query on the page, including menus and widgets, so always guard with $query->is_main_query() and a conditional like is_home(), or you will break things you didn’t intend to touch. And the_content runs on every post body including feeds and excerpts in some contexts, so guard with is_single() when your change is meant for single posts only.
Debugging Hooks: Why Your Hook Doesn’t Fire
This is the section most guides leave out, and it’s the one you’ll actually need. When a hook does nothing, work through this list in order:
- Wrong timing. Your callback is registered after the hook already fired. Code at the top level of a plugin file runs while plugins are still loading — the current user isn’t known yet, the theme isn’t loaded, post types aren’t registered. If your hook depends on something that doesn’t exist yet, register it from a later hook instead.
- Wrong priority on removal. You called
remove_action()with priority 10, but the original registration used priority 20. Removal is an exact match — it silently returnsfalse. - Wrong
accepted_args. Your callback declares three parameters but you registered the default of one. PHP throws an argument-count error, or your extra parameters arrive empty. - A conditional that never becomes true.
is_single()and friends don’t work before the main query runs. A filter hooked toinitthat checksis_page()will always fail, because the query doesn’t exist yet atinit. - A filter that doesn’t return. Your callback echoes something and forgets
return $value. The filtered value becomesnulland the output disappears.
WordPress ships a few inspection functions that make this concrete. did_action( 'save_post' ) tells you how many times an action has fired (zero means it hasn’t fired yet — your code ran too early, or the event never happened). has_action( 'init', 'my_callback' ) tells you whether a specific callback is registered, and returns its priority. current_filter() inside a shared callback tells you which hook is currently calling it.
For a visual answer, install Query Monitor — its Hooks & Actions panel lists every hook that fired on the current page, in order, with the callbacks attached to each and the component that registered them. When a hook doesn’t fire, the panel shows you it never appeared; when the wrong callback runs, it shows you exactly which one and where it came from. It is the single most useful debugging plugin for WordPress development, and it stays installed on every site I build.
One more trick for the truly stuck: hook into all. WordPress fires a pseudo-hook named all for every single hook, so a callback on all that logs current_filter() gives you a complete trace of the request’s hook order. Never ship that in production — it fires hundreds of times per request — but for fifteen minutes of debugging, it ends arguments about what runs when.
Build Your Own Hooks: Making Your Code Extensible
Hooks aren’t only for consuming WordPress’s extension points. If you write a plugin or theme, adding your own hooks is what makes other developers able to extend your work without forking it. The pattern is two lines:
// Fire a custom action in your plugin:
do_action( 'htsi_after_booking_saved', $booking_id );
// Let others filter a value your plugin produces:
$price = apply_filters( 'htsi_booking_price', $price, $booking_id );
Three rules make custom hooks good citizens. Prefix the hook name with your plugin slug so it never collides with another plugin’s hook. Use filters for values and actions for moments — if the caller needs to change a value, it’s a filter; if they need to react to something happening, it’s an action. And document every custom hook: its name, when it fires, and what arguments it passes. A hook nobody can discover might as well not exist — the Code Reference and a quick grep for do_action in your plugin are how other developers find them.
7 WordPress Hook Mistakes to Stop Making
- Editing core or a parent theme instead of hooking. Your change dies at the next update.
- Forgetting
returnin a filter. The filtered value becomesnulland content vanishes. - Wrong
accepted_args. Declaring parameters you never registered for, or registering for parameters you never declared. - Hooking too early or too late. Registering post types outside
init, or checking conditionals before the query exists. - Removing a hook with the wrong priority — or before it was registered — and getting a silent
false. - Using closures for callbacks you expect others to remove. Anonymous functions can’t be unhooked.
- Putting business logic in a filter. Filters should be pure: value in, value out. Side effects — emails, database writes — belong in actions.
The Bottom Line
WordPress hooks are a small idea with a large payoff: two functions to attach (add_action, add_filter), two to fire (do_action, apply_filters), and two numbers — priority and accepted arguments — that decide the order and the data. Learn those, keep the hook reference open while you work, and install Query Monitor for the days a hook won’t fire. Everything else in WordPress development — plugins, themes, the whole ecosystem — is just hooks all the way down.
Verified against the official WordPress Plugin Handbook and Code Reference, 15 November 2026.
Further Reading & References
- Hooks — WordPress Plugin Handbook (official)
- Actions — WordPress Plugin Handbook (official)
- save_post_{$post_type} — WordPress Code Reference (official)
- New action wp_after_insert_post in WordPress 5.6 — Make WordPress Core
- Query Monitor — WordPress plugin directory
- WordPress Theme Hooks, Actions and Filters Explained — Markup Dude
- WordPress Hooks Explained: A Beginner’s Guide — Savvy
- Mastering WordPress Hooks: Actions and Filters — Reintech



