Hooks are how WordPress lets you change what it does without touching its core files, your theme’s templates or someone else’s plugin. They’re the reason thousands of plugins can work on the same site at once. If you’ve ever wondered whether hooks count as real code, how actions differ from filters or why your function runs at the wrong time, this walkthrough covers all of it, with working PHP examples. It’s written for site owners, freelancers and developers in the US, UK, Canada, Australia and Europe.
Quick answer: WordPress hooks are points in WordPress code where you can attach your own functions. There are two types. Actions run your function at a specific moment, like when a post is saved, and return nothing. Filters receive a value, let you change it and must return it. You attach functions with
add_action()oradd_filter(), usually in a small plugin or a child theme.
Table of Contents
What Are WordPress Hooks?
WordPress hooks are named points in WordPress core, themes and plugins where your own code can run or change data. WordPress calls each hook at a set moment, and any function attached to it runs then. This lets you customize a site safely, without editing core files that updates would overwrite.
Actions and Filters in One Sentence Each
Actions let you do something at a specific moment, such as adding a script to the page head or sending an email when a post is published. Filters let you change something before WordPress uses it, such as post content, an excerpt length or a login error message. Both are part of WordPress’s Plugin API hooks system, which says filters should return a value and avoid side effects.
Where Hooks Come From
WordPress core fires hundreds of hooks. Themes and plugins add their own, so WooCommerce, for example, has hooks for the cart and checkout. When code calls do_action() or apply_filters() with a name, that becomes a hook other code can attach to.
Are WordPress Hooks Coding Mechanisms?
Yes. WordPress hooks are a coding mechanism, specifically an event-driven system built into WordPress’s PHP code. Using them means writing PHP functions and registering them with add_action() or add_filter(). They don’t require deep programming skill for simple tasks, but they are code, and mistakes in them can break a site.
How Hooks Fit Into WordPress Development
Hooks are the main way plugins and themes extend WordPress. Our guide on how to develop WordPress plugins shows that nearly every plugin starts by attaching functions to hooks. Theme developers use them too, which our WordPress theme development guide covers.
Do You Need to Code to Use Hooks?
You need to add a little PHP, but you don’t need to be an expert. Plugins like WPCode let you paste snippets safely, and a simple site plugin works well for a handful of hooks. For anything beyond a few lines, test on a staging site first.
WordPress Action Hooks Explained
WordPress action hooks run your function at a specific point, such as when the page head loads, a post is saved or a user logs in. Your function does its job, like printing HTML or saving data, and returns nothing. You attach it with add_action(), giving the hook name and your function.
Action Hook Example
This example adds a notice right after the opening body tag, using the wp_body_open action, which most modern themes support.
function lauren_order_cutoff_notice() {
echo '<div class="cutoff-notice">Holiday box orders close Friday at noon.</div>';
}
add_action( 'wp_body_open', 'lauren_order_cutoff_notice' );
Common Action Hooks
- init: runs after WordPress loads. Use it to register custom post types and taxonomies.
- wp_enqueue_scripts: the right place to load front-end CSS and JavaScript.
- wp_head and wp_footer: print code in the page head or footer, such as tracking tags.
- save_post: runs when a post is saved, useful for updating custom fields.
- pre_get_posts: changes a query before it runs, for example to exclude a category from the blog page.
WordPress Filter Hooks Explained
WordPress filter hooks pass a value through your function so you can change it. Your function receives the value, modifies it and must return it, even if you change nothing. Filters should not cause side effects like printing output or saving data. You attach them with add_filter().
Filter Hook Example
This example shortens automatic excerpts using the excerpt_length filter, which defaults to 55 words.
function lauren_shorter_excerpts( $length ) {
return 30;
}
add_filter( 'excerpt_length', 'lauren_shorter_excerpts' );
Common Filter Hooks
- the_content: changes post content before it’s displayed.
- excerpt_length: changes the number of words in automatic excerpts.
- body_class: adds CSS classes to the body tag.
- login_errors: changes the message shown when login fails, often to hide which part was wrong.
- upload_mimes: controls which file types users can upload.
How WordPress Hooks Work: Priority and Arguments
WordPress hooks run attached functions in order of priority, from lowest number to highest, with 10 as the default. Functions with the same priority run in the order they were added. The fourth parameter, accepted arguments, tells WordPress how many values to pass to your function.
The add_action Signature
The official add_action reference shows the full signature. add_filter() takes the same parameters.
add_action( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 );
Priority and Accepted Arguments Example
The save_post action passes three values: the post ID, the post object and whether it’s an update. To receive all three, set accepted arguments to 3.
function lauren_log_product_save( $post_id, $post, $update ) {
if ( wp_is_post_revision( $post_id ) || 'product' !== $post->post_type ) {
return;
}
update_post_meta( $post_id, '_last_checked', current_time( 'mysql' ) );
}
add_action( 'save_post', 'lauren_log_product_save', 20, 3 );
How to Remove and Create WordPress Hooks
You can remove a function from a hook with remove_action() or remove_filter(), and you can create your own hooks with do_action() and apply_filters(). Removing hooks is how you switch off a plugin or theme feature without editing its files. Creating hooks makes your own code extendable by others.
Removing a Hook
WordPress’s advanced hooks guide explains that removal must use the same priority the function was added with, and must run after it was added. That’s why removals often sit inside another hook, such as init or wp_loaded.
function lauren_remove_parent_footer() {
remove_action( 'wp_footer', 'parent_theme_footer_credit', 10 );
}
add_action( 'init', 'lauren_remove_parent_footer' );
Creating Your Own Hook
The custom hooks guide recommends prefixing hook names to avoid clashes. Here’s a custom filter another developer could use to change the cutoff text.
$message = apply_filters( 'lauren_cutoff_message', 'Holiday box orders close Friday at noon.' );
echo esc_html( $message );
Where to Put WordPress Hooks Code
Put WordPress hooks code in a small custom plugin or a child theme’s functions.php file, never in WordPress core or a parent theme you don’t control. A custom plugin keeps your changes working even if you switch themes. A child theme suits changes tied to that theme’s design.
Your Options Compared
| Location | Survives theme updates | Survives a theme switch | Best for |
|---|---|---|---|
| Custom site plugin | Yes | Yes | Site features like notices and checkout tweaks |
| Child theme functions.php | Yes | No | Design changes tied to one theme |
| Code snippets plugin | Yes | Yes | A few small snippets, non-developers |
| Parent theme functions.php | No | No | Never recommended |
Test Before You Go Live
Write and test hooks on a local or staging copy first. Our guide to WordPress local development shows how to set one up. If a mistake causes a blank screen, our fix for the WordPress white screen of death will help you recover.
Common WordPress Hooks Mistakes
The most common WordPress hooks mistakes are forgetting to return a value in a filter, using the wrong priority, removing a hook before it was added and loading scripts on the wrong hook. Each one causes silent failures that are hard to spot, so test every change on a staging site.
Mistakes to Avoid
- Not returning in a filter. A filter that returns nothing blanks the value, such as the whole post content.
- Echoing in a filter. Filters should return values, not print them.
- Wrong accepted arguments. If you need the second or third value, raise the fourth parameter.
- Removing too early.
remove_action()only works after the function was added, with the same priority. - Generic function names. Prefix your functions to avoid clashes with plugins.
- Loading scripts on init. Use
wp_enqueue_scriptsfor front-end assets.
Debugging Hooks
Use did_action() to check whether an action has already fired, and current_filter() to see which hook is running. The free Query Monitor plugin lists the hooks fired on each page and which functions are attached to them. Our list of WordPress interview questions covers more of these concepts for developers.
FAQs
These are the WordPress hooks questions developers and site owners ask us most.
What are hooks in WordPress?
Hooks in WordPress are named points in the code where you can attach your own functions to run or to change data. WordPress core, themes and plugins all fire hooks at set moments, such as loading the page head or saving a post. There are two types, actions and filters. Hooks let you customize a site without editing core files, so your changes survive WordPress updates.
What is the difference between action and filter hooks?
An action hook runs your function at a specific moment so it can do something, like print HTML, send an email or save data, and it doesn’t return anything. A filter hook passes a value into your function so you can change it, and your function must return that value. Use actions to add behavior and filters to modify content, settings or output before WordPress uses them.
Are WordPress hooks coding mechanisms?
Yes. WordPress hooks are an event-driven coding mechanism built into WordPress’s PHP code, part of what the developer documentation calls the Plugin API. Using them means writing PHP functions and registering them with add_action or add_filter. Simple hooks only need a few lines of code, but they still run on your live site, so test them on a staging copy before adding them to production.
What does add_action do in WordPress?
The add_action function attaches your function to an action hook so it runs when WordPress fires that hook. It takes the hook name, your callback function, an optional priority that defaults to 10 and an optional number of accepted arguments that defaults to 1. Lower priority numbers run earlier. Functions with the same priority run in the order they were added to the hook.
What is hook priority in WordPress?
Hook priority is a number that decides when your function runs compared with others on the same hook. The default is 10, lower numbers run earlier and higher numbers run later. Functions with equal priority run in the order they were added. Use a higher priority, such as 20 or 99, when you need to run after a plugin or theme function that uses the default.
How do I remove an action in WordPress?
Call remove_action with the same hook name, function name and priority that were used to add it. It only works after the original function was added, so wrap the removal in another hook that runs later, such as init or wp_loaded. If a plugin added its function using a class method, you’ll need the same object instance or class reference to remove it successfully.
Where should I add WordPress hook code?
Add hook code to a small custom plugin for site features, or to a child theme’s functions.php for changes tied to that theme’s design. Never edit WordPress core or a parent theme, because updates overwrite those files. A code snippets plugin also works for a few short snippets. Keep your code in version control and test every change on a staging site before going live.
Can I create my own WordPress hooks?
Yes. Call do_action with a unique name to create a custom action, or apply_filters with a name and a value to create a custom filter. Other developers, or your future self, can then attach functions without editing your code. Prefix custom hook names with your plugin or theme slug to avoid clashes, and document what each hook does and what arguments it passes.
Why is my WordPress filter not working?
The most common causes are a filter function that doesn’t return a value, a priority that runs before another function overrides yours, or a wrong number of accepted arguments. Check that your file is actually loading, that the hook name is spelled correctly and that the hook fires on that page. Query Monitor shows which hooks fired and which functions are attached, which speeds up debugging.
What are the most used WordPress hooks?
Common action hooks include init, wp_enqueue_scripts, wp_head, wp_footer, save_post and pre_get_posts. Common filter hooks include the_content, the_title, excerpt_length, body_class and login_errors. WooCommerce stores also use many WooCommerce-specific hooks for the cart, checkout and product pages. Learning these core hooks first covers most everyday customization work on business sites and blogs. The developer reference lists every core hook with its arguments and the version that added it.
Conclusion
We moved Lauren’s order cutoff notice and checkout message out of her theme files and into a small site plugin built on WordPress hooks. The notice now hooks into wp_body_open, the checkout message uses a WooCommerce hook, and the cutoff text runs through a custom filter, so it can be changed in one place. Since then, her theme has updated without wiping anything, and customers see the cutoff warning before they order. She hasn’t paid for the same fix again, and any change to the notice before a holiday rush now takes one small edit in her site plugin.
That’s the real value of WordPress hooks. Actions let you add behavior at the right moment, filters let you change data safely, and both keep your custom code separate from files that updates replace. Start with one small change in a site plugin, test it on staging and build from there. Which theme edit on your site would you move into a hook first?











