WordPress hooks and filters let you change site behavior without editing WordPress core, a theme, or a plugin directly. This guide explains the difference between actions and filters, shows how to choose a suitable hook, and provides a repeatable workflow for testing custom code safely across content, queries, admin screens, authentication, and WooCommerce.
Overview
A hook is a named point in WordPress execution where your code can connect to an existing process. WordPress uses two main types: actions and filters. Choosing the correct type is the first step in a reliable customization.
| Question | Use an action | Use a filter |
|---|---|---|
| What does the callback do? | Runs a task at a particular moment. | Receives a value, changes it, and returns it. |
| Must the callback return a value? | Usually no. | Yes. Return the filtered value. |
| Typical uses | Registering assets, adding metadata, sending a notification, or setting up an admin screen. | Changing text, markup, query arguments, settings, or returned data. |
| Common functions | add_action() and do_action() | add_filter() and apply_filters() |
In practice, actions and filters are both implemented through WordPress's hook system. The important distinction is how the hook is intended to be used. An action gives your callback an opportunity to do something. A filter gives your callback a value to inspect and modify.
For example, an action is appropriate when enqueuing a stylesheet:
function ccs_enqueue_site_styles() {
wp_enqueue_style(
'ccs-site',
get_stylesheet_directory_uri() . '/assets/site.css',
array(),
'1.0.0'
);
}
add_action( 'wp_enqueue_scripts', 'ccs_enqueue_site_styles' );A filter is appropriate when changing a generated title:
function ccs_add_site_label( $title ) {
if ( is_singular() ) {
$title .= ' | Code Craft Studio';
}
return $title;
}
add_filter( 'the_title', 'ccs_add_site_label' );The second example must return $title. Omitting that return statement can cause the value to disappear or produce unexpected output.
How to compare options
When deciding how to customize WordPress, begin with the desired result rather than with a familiar hook name. Use this decision process:
- Describe the change in one sentence. For example: “Add a class to selected menu items” or “Exclude a private category from the main query.”
- Identify whether you are observing an event or changing a value. If you need to run a task, look for an action. If you need to alter data already being prepared, look for a filter.
- Locate the value or event's source. Search the relevant WordPress, theme, or plugin code for
do_action(),apply_filters(),do_action_ref_array(), orapply_filters_ref_array(). - Check the callback signature. Confirm how many arguments are passed and which argument contains the value you need.
- Choose a priority deliberately. A lower priority number generally runs earlier; a higher number generally runs later. The default priority is commonly 10, but your code should not rely on a priority without understanding the surrounding callbacks.
- Test the complete request. A hook may run on the front end, in the administration area, during a REST request, or only for a particular post type or WooCommerce screen.
Hook names alone are not enough. A filter may exist but expose a value before the information you need has been added. An action may run at the right time but provide no useful arguments. Read the hook's documentation and, when necessary, inspect the source that fires it.
Keep customizations in a small site-specific plugin when they should remain independent of the active theme. A child theme can be suitable for presentation-related changes, but theme updates and theme switching are reasons to separate reusable behavior. See this WordPress child theme tutorial for a safer theme customization workflow.
Feature-by-feature breakdown
Content and markup
Use content filters when WordPress has already prepared text and you need to modify the result. For example, adding a note after the content can be done with the_content:
function ccs_append_reading_note( $content ) {
if ( is_singular( 'post' ) && in_the_loop() && is_main_query() ) {
$content .= '<p class="reading-note">Updated guidance may vary by site configuration.</p>';
}
return $content;
}
add_filter( 'the_content', 'ccs_append_reading_note' );Use conditional checks to avoid adding the same output to feeds, excerpts, secondary loops, or unrelated post types. Escape dynamic values at output time and sanitize data when it is stored or accepted from a user.
Queries
Query filters are useful when you need to adjust query arguments before WordPress runs a query. A common example is modifying the main archive query:
function ccs_adjust_blog_archive( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
$query->set( 'posts_per_page', 12 );
}
}
add_action( 'pre_get_posts', 'ccs_adjust_blog_archive' );pre_get_posts is an action even though the callback changes a query object. The callback is acting on the query before execution rather than returning a replacement query object. Avoid changing every query indiscriminately; WordPress, plugins, feeds, widgets, and REST requests may create additional queries.
Admin screens and assets
Actions are normally used to register admin menus, enqueue scripts, or add settings fields. Keep admin-only code behind an appropriate condition and load assets only on the screen that needs them. This reduces unnecessary work and limits the chance of affecting unrelated screens.
For front-end assets, wp_enqueue_scripts is generally the intended action. For administration assets, use the administration enqueue action and inspect the screen identifier before loading a script. Registering assets directly in a template is harder to maintain and can create ordering problems.
Authentication and user behavior
Authentication-related hooks require extra care because a small change can affect every login, logout, or user request. Use an action for an event such as recording a successful login in a controlled, privacy-conscious way. Use a filter when WordPress exposes a value that should be changed, such as a login redirect or an authentication result.
Never treat a hook as a substitute for authorization checks. A callback that changes access, user roles, or redirects should verify capabilities, validate input, and account for failure paths. Test administrator, editor, subscriber, logged-out, and invalid-login scenarios separately.
WooCommerce
WooCommerce provides many actions and filters for product data, cart behavior, checkout fields, order processing, and customer emails. The same rule applies: use an action to run a task and a filter to modify a value. Before using a WooCommerce hook, confirm whether it runs once per request, once per item, or once per field.
Checkout changes often involve several related hooks. A field can be registered in one location, validated during checkout, saved to order data, and displayed later through another hook. Changing only the visible field may leave validation or persistence incomplete. For a complete example, review this guide to customizing WooCommerce checkout fields safely.
Priority and accepted arguments
Priority controls callback order. If two callbacks modify the same value, your callback may need to run after another callback has made its change. Set a priority only when there is a reason, document that reason, and retest after plugin or theme updates.
The fourth argument of add_filter() and add_action() declares how many arguments your callback accepts:
function ccs_change_archive_title( $title, $term_name ) {
return $title . ': ' . $term_name;
}
add_filter( 'get_the_archive_title', 'ccs_change_archive_title', 10, 2 );If the accepted-argument count is wrong, your callback may receive too few values or trigger an argument-related error. Confirm the hook's documented signature instead of guessing from a similarly named hook.
Best fit by scenario
| Customization goal | Likely starting point | What to verify |
|---|---|---|
| Add a stylesheet or JavaScript file | Action such as wp_enqueue_scripts | Dependencies, versioning, and the pages where the asset should load |
| Change post or page text | Filter such as the_content or the_title | Loop context, excerpts, feeds, and repeated application |
| Modify an archive query | Action such as pre_get_posts | Main query checks, admin requests, post type, and taxonomy conditions |
| Add an admin menu or settings field | Administration action | Capability checks, menu location, and settings sanitization |
| Change a WooCommerce field or order value | WooCommerce action or filter | Validation, saving, display, checkout updates, and extension compatibility |
| Change a REST response | REST-related filter or endpoint action | Authentication, permissions, response shape, and escaping |
If you cannot find a suitable hook, do not immediately edit a plugin file. First check whether the feature provides a documented extension point, then inspect the relevant source in a local or staging environment. A small custom plugin is usually easier to version, disable, and test than scattered snippets added through multiple interfaces.
For debugging, add temporary logging only in a non-public environment and avoid recording passwords, tokens, personal data, or complete request payloads. Confirm that the callback is registered, the hook executes in the request you are testing, the conditions evaluate as expected, and the callback returns the required value. Plugin conflicts can often be isolated by staging changes and disabling unrelated customizations one at a time; this plugin conflict diagnosis guide provides a broader workflow.
When to revisit
Revisit hook-based customizations whenever WordPress, the active theme, WooCommerce, or another extension changes a related feature. Updates can alter markup, execution order, accepted arguments, block-editor behavior, or the point at which data becomes available. A hook that worked for a classic template may not control output generated by a block, a REST request, or a JavaScript-driven interface.
Use this maintenance checklist:
- Record the hook name, callback purpose, priority, accepted-argument count, and location of the custom code.
- Test the intended front-end, admin, REST, and WooCommerce paths on staging when they apply.
- Check PHP compatibility and review the error log for warnings, deprecated functions, and fatal errors.
- Verify that filters still return the original or modified value and that actions do not run more often than expected.
- Test logged-in and logged-out states, different user capabilities, mobile layouts, and relevant languages or currencies.
- Measure whether the customization adds avoidable queries, repeated processing, or front-end assets on pages that do not need them.
- Document any dependency on priority or another plugin's callback so the next update can be assessed quickly.
Keep a staging copy and a rollback path before changing production code. When a hook stops firing, compare the current source and request flow with the version that worked, then verify whether the feature has moved into a block, REST endpoint, or replacement API. This habit makes WordPress customization more predictable and turns a one-off code snippet into a maintainable developer workflow.