WordPress Hooks Explained: Actions vs Filters
- #wordpress
- #php
- #hooks
- #webdevelopment
One of WordPress's most important architectural features is its hook system.
Hooks allow plugins and themes to interact with WordPress at specific points in its execution without modifying the core files. They are the foundation of many customizations, from adding content to changing query behavior and integrating external services.
WordPress provides two primary hook types: actions and filters.
They share a similar registration mechanism, but they serve different purposes.
1. Actions: execute behavior at a specific point
An action allows code to run when WordPress reaches a particular event or execution point.
Use an action when you want to perform an operation, such as registering functionality, recording an event, or adding content to a hook location.
For example:
add_action('init', 'myplugin_register_feature');
function myplugin_register_feature(): void {
// Register custom functionality here.
}WordPress calls the registered callback when the init action runs.
The callback performs an operation; it does not return a replacement value to the action system.
Actions are commonly used for:
- Registering custom post types and taxonomies.
- Enqueuing scripts and styles.
- Adding administrative functionality.
- Responding to lifecycle events.
- Integrating functionality at supported extension points.
The event itself determines when the callback executes.
2. Filters: transform a value
A filter allows code to receive a value, modify it, and return the result.
For example, a plugin may customize the title displayed for a specific type of content.
add_filter('the_title', 'myplugin_customize_title', 10, 2);
function myplugin_customize_title(
string $title,
int $post_id
): string {
if (is_admin()) {
return $title;
}
if (get_post_type($post_id) !== 'book') {
return $title;
}
return 'Book: ' . $title;
}This illustrative callback prefixes the title of book posts on the front end.
The callback must return a value. If it does not return the expected value, subsequent code may receive an incorrect result.
Filters are useful for modifying:
- Displayed content.
- Generated HTML or text.
- Query arguments.
- Plugin or theme settings.
- Supported configuration values.
Always verify the arguments and expected return value of the specific filter you are using.
3. The key difference
| Characteristic | Action | Filter |
|---|---|---|
| Main purpose | Execute behavior | Transform a value |
| Typical callback | Performs an operation | Receives and returns a value |
| Registration | add_action() | add_filter() |
| Execution | do_action() | apply_filters() |
| Return value | Not used as a transformed result | Returned value is passed along |
Internally, WordPress uses a shared hook infrastructure for both. The practical distinction is the contract expected from the callback.
4. Understand priorities and accepted arguments
Both add_action() and add_filter() support a priority and a number of accepted arguments.
Consider:
add_action('init', 'myplugin_first_callback', 10);
add_action('init', 'myplugin_second_callback', 20);Callbacks with lower priority numbers normally execute first. The default priority is 10.
The accepted-arguments parameter controls how many arguments WordPress passes to the callback.
For example:
add_filter('the_title', 'myplugin_title_callback', 10, 2);This registration requests up to two arguments for the callback.
The callback signature must match the arguments actually provided by the hook. Registering a callback for more arguments does not create additional data; it only determines how many available arguments may be passed.
5. Use hooks instead of editing core files
Modifying WordPress core files is fragile because updates can overwrite those changes and make future maintenance difficult.
A plugin or a child theme is generally a better place for custom behavior.
A useful decision process is:
- Identify the behavior or value you need to change.
- Find an existing WordPress hook that supports the change.
- Check the hook's documentation and arguments.
- Register a callback in an appropriate plugin or theme location.
- Test the result with other active plugins and themes.
For functionality that should remain available regardless of the active theme, a dedicated plugin is often more appropriate than a theme's functions.php.
6. Avoid common mistakes
Forgetting to return a filtered value
A filter callback should return the transformed value, even when no modification is necessary.
Using the wrong hook
An action is not a substitute for a filter when another component expects a modified value.
Assuming every hook provides the same arguments
Hook signatures vary. Inspect the documentation for the exact hook.
Registering callbacks too late
Some hooks run early in the request lifecycle. Registering functionality after the relevant hook has already executed may have no effect.
Performing expensive work unnecessarily
Hooks can run frequently. Avoid heavy database operations, repeated remote API calls, or other costly processing inside callbacks unless the behavior genuinely requires it.
Ignoring security
Escape output according to its context, sanitize incoming data appropriately, and check authorization and nonces when handling state-changing requests.