WordPress Hooks Explained: Actions and Filters

Every WordPress tutorial eventually tells you to hook into something. The word sounds technical, but the idea is friendly. This guide gives you WordPress hooks actions filters explained in plain language, with small examples you can run safely and mistakes you can avoid from day one.
Hooks are the reason you can change WordPress without editing its core files. Themes and plugins use them constantly, and your own snippets will too. Once you understand the two hook types and how to register a function, most tutorial code stops looking like magic and starts reading like simple instructions.
What Is a WordPress Hook?
A hook is a named point where WordPress pauses and lets your code join in. WordPress reaches that point, runs every function registered for that hook name, and then continues. Your function is called a callback. You never call the callback yourself. WordPress calls it at the right moment.
Hook names describe their moment. wp_footer fires near the end of the page, just before the closing body tag. the_content filters post content before display. body_class filters the list of classes on the body tag. Descriptive names help you guess where a hook acts before you read its documentation.
What Is the Difference Between Actions and Filters?
WordPress has exactly two hook types. Actions let you do something at a moment. Filters let you change a value as it passes through. The difference sounds small, yet it decides how you write the callback and what WordPress expects back from it.
- Action: runs at a point in the page load and usually prints output or performs a task.
- Filter: receives a value, modifies it, and must return the value for WordPress to use.
- Action example: print a notice in the footer with the wp_footer action.
- Filter example: append a line to post content with the the_content filter.
- Return rule: actions return nothing, filters always return the filtered value.
A simple memory aid helps. Actions act, filters filter. To add output or run a task at a moment, use an action. To change text, a list, or a URL that WordPress prepared, use a filter. Choosing wrong causes the classic bug where output lands in the wrong place or a value turns empty.
How Do add_action and add_filter Work?
You register a callback with add_action for actions and add_filter for filters. Both functions accept the same four arguments. Learn these four once and you can read almost any hook registration you meet in tutorials, themes, and plugins.
add_action('hook_name', 'your_callback_function', 10, 1);
add_filter('hook_name', 'your_callback_function', 10, 1);
- Hook name: the exact string WordPress fires, such as wp_footer or the_content.
- Callback: the name of your function, written as a string in quotes.
- Priority: a number controlling run order, where lower numbers run earlier.
- Accepted args: how many values WordPress should pass into your callback.
The last two arguments have defaults. Priority defaults to 10 and accepted args defaults to 1, so short registrations omit them. Include them when order matters or when a hook passes extra values your callback needs. A filter such as the_content passes one value, while other hooks pass several, such as an ID plus an object.
Your callback is a normal PHP function. Name it with a unique prefix so it never collides with a theme or plugin function. Prefixes like vutg_ keep your code distinct. If two functions share one name, PHP stops with a fatal error, and the site shows a critical error screen instead of your page.
Example 1: How Do You Add Text With the wp_footer Action?
Actions suit output. The wp_footer action fires near the end of every front end page, so it is perfect for a small footer note. Register a callback that echoes your text. Echo is correct here because an action prints directly at its moment in the page.
add_action('wp_footer', 'vutg_footer_note');
function vutg_footer_note() {
echo '<p class="vutg-footer-note">Thanks for visiting our site.</p>';
}
Save, reload a page, and scroll down. Your note appears after the main content. You never edited a template and never called the function yourself. WordPress reached wp_footer and called it for you. Guard output with a condition such as is_front_page when it should appear only in some places.
Example 2: How Do You Append Text With the the_content Filter?
Filters suit changes to existing values. The the_content filter receives the post content as a string before display. Your callback adds a line to that string and returns the full result. Returning is not optional. WordPress displays exactly what your callback returns.
add_filter('the_content', 'vutg_append_content_note');
function vutg_append_content_note($content) {
if (!is_singular('post') || !in_the_loop()) {
return $content;
}
$note = '<p>Enjoyed this post? Share it with a friend.</p>';
return $content . $note;
}
Read the guard closely. is_singular limits the change to single post views, and in_the_loop confirms WordPress is rendering the main post rather than a widget or excerpt. Without guards, the note can appear in feeds, excerpts, and unexpected places. Good filters check context, change only what they must, and return the untouched value everywhere else.
Compare this with the footer example. The action echoed. The filter returned. Swapping those habits breaks pages. Echo inside a filter prints at the wrong time, often above the header, because filters run while WordPress prepares values, not while it prints the page. Always ask which type you registered before writing the callback body.
Example 3: How Does the body_class Filter Show the Return Rule?
The body_class filter receives an array of CSS classes for the body tag. Add your class to the array and return the array. This example shows why filters must return the same type they received. An array goes in, so an array must come out.
add_filter('body_class', 'vutg_add_body_class');
function vutg_add_body_class($classes) {
if (is_front_page()) {
$classes[] = 'vutg-homepage';
}
return $classes;
}
View the homepage source and find your class on the body tag, then target it with CSS for homepage only styling. Append to the value instead of replacing it, so classes from WordPress, your theme, and other plugins survive.
Forgetting the return line is the classic failure. WordPress receives null, the body tag loses its classes, and layout rules tied to those classes stop working. When a filter seems to erase content, check the return statement first. The fix is usually one missing line.
How Does Priority Control the Run Order?
Several callbacks can share one hook. Priority decides their order. Lower numbers run earlier, and the default is 10. Callbacks with equal priority run in registration order. This matters when one callback must prepare something before another uses it, or when your change must happen after a plugin change.
add_action('wp_footer', 'vutg_runs_first', 5);
add_action('wp_footer', 'vutg_runs_second', 20);
For filters, priority controls the chain of changes. Each callback receives the value left by the previous one. When your change seems overwritten, a later priority callback may be replacing it. Adjust your priority deliberately and retest. Avoid extremes without a reason: most beginner code belongs at 10.
How Do You Remove a Hook With remove_action and remove_filter?
Sometimes you need to stop a callback that a theme or plugin registered. remove_action and remove_filter unregister it. Both need the hook name, the callback name, and the same priority used at registration. Priority matching is strict. A removal with the wrong priority silently does nothing.
remove_action('wp_footer', 'vutg_footer_note', 10);
remove_filter('the_content', 'vutg_append_content_note', 10);
Timing matters too. Your removal must run after the original registration. Wrap removals in a later hook such as init or after_setup_theme, depending on when the target registered. Removing too early finds nothing to remove. When a removal fails, check three things in order: exact callback name, exact priority, and removal timing.
How Do You Find the Right Hook Name?
Start with the official developer reference at developer.wordpress.org. Search the hook name or the task you have in mind. Each hook page shows the parameters passed, the source location, and often related hooks. Reading the parameter list prevents accepted args mistakes before they happen.
Common hooks cover most beginner goals. wp_enqueue_scripts loads styles and scripts. init suits early setup tasks. wp_footer prints near the page end. the_content changes post content. body_class adjusts body classes. the_title changes post titles. Learn these six and you can follow a large share of beginner tutorials without guessing.
Hooks also connect to wider WordPress systems. The REST API, for instance, registers routes during the rest_api_init action. Our guide to the WordPress REST API explained for beginners shows that pattern in context. Seeing one hook inside a bigger feature makes the concept stick.
What Hook Mistakes Break Sites Most Often?
- Forgetting to return in a filter, which replaces content with nothing.
- Echoing inside a filter, which prints output in the wrong place.
- Removing a callback with the wrong priority, which changes nothing.
- Using a callback name without a prefix, which risks a fatal name clash.
- Skipping context checks, which leaks changes into feeds and widgets.
- Accepting fewer args than needed, which leaves values undefined.
Debug in a fixed order. Confirm the hook name spelling, check for syntax errors, then inspect the callback: filters return, actions echo or act. Finally check priority and context guards. Keep callbacks small and single purpose, so one behaviour can be disabled at a time.
Where Should You Put Hook Code Safely?
Never place hook code in a parent theme or in WordPress core files. Updates overwrite those files and erase your work. Use a child theme functions.php file or the Code Snippets plugin. Both survive updates, and Code Snippets lets you disable a broken snippet quickly from the dashboard.
Our step by step guide to create a WordPress child theme sets up the safest home for theme level code. For scripts that hooks enqueue, our tutorial on how to add custom JavaScript in WordPress without a plugin shows the enqueue pattern that wp_enqueue_scripts expects.
Test on staging first, keep a backup, and add one snippet at a time so a failure points to its cause. Reliable hosting makes testing calmer, because slow servers hide timing issues. Our web hosting guide explains what a growing site needs.
Frequently Asked Questions
Can one function be both an action and a filter?
Technically yes, because both share one system. In practice, keep them separate. Write actions that act and filters that return values, so each callback has one clear contract and stays easy to debug.
Why is my filter callback never running?
Check the hook name spelling first, then confirm your snippet is active and runs on the front end. A callback registered for the wrong hook name waits forever. Also confirm the page you test actually triggers that hook.
What priority should beginners use?
Use the default priority of 10 unless order matters. Change it only when you must run before or after a specific callback, and note the reason in a comment so the choice stays clear later.
Is Code Snippets safe for hook code?
Yes, when used carefully. It keeps code out of theme files, survives updates, and lets you disable a snippet fast. Test on staging, run PHP snippets on the front end only when suitable, and keep backups.
Last updated: December 2026.



