General, WooCommerce, WordPress
A Quick Guide to WordPress Hooks
By Goshila Sadaf · · Updated
WordPress hooks are the foundational backbone of modern WordPress development. They allow developers to execute custom code at specific execution points across the core, themes, and plugins—without modifying core files.
If you want to build custom features, alter database outputs, or extend plugin functionality safely, mastering WordPress hooks is essential. In this guide, we will break down what hooks are, explain the critical differences between Actions and Filters, and walk through practical code examples you can use immediately.
Related: WooCommerce Pages Hooks: A Comprehensive Guide
What is a WordPress hook?
A WordPress Hook is a programmatically defined marker within the WordPress core execution flow. It gives your theme or plugin a way to “hook into” the default execution, allowing you to either execute custom code or modify existing data before it is rendered on screen or stored in the database.
WordPress offers two types of hooks:
Actions (add_action)
Filters (add_filter)
What is an action in WordPress?
An action is a WordPress hook that is triggered at a specific point while WordPress is running and lets your code perform a task there, such as creating a widget when WordPress is initializing.
What is a filter in WordPress?
Filters are WordPress hooks that allow modification of data or content before it is displayed or processed. They provide an opportunity to alter values or customize the output.
Actions vs. Filters: Key Differences
Knowing when to use a WordPress action versus a WordPress filter is the most critical step in WordPress development.
| Feature | Action Hooks | Filter Hooks |
|---|---|---|
| Primary Goal | Perform a task or add functionality. | Modify or intercept data. |
| Return Value | Does not return a value (void). |
Must return a modified value. |
| Common Use Cases | Injecting scripts/styles, sending emails, saving metadata. | Trimming excerpts, modifying page titles, altering queries. |
| WordPress Function | add_action() |
add_filter() |
9 Essential WordPress Hooks Every Developer Should Know
Below are the most frequently used WordPress core hooks, categorized by function:
Header and Footer Hooks
wp_head (Action): Fires inside the <head> section of your theme. Used to inject meta tags, tracking pixels, or inline styles into the frontend.
add_action( 'wp_head', 'my_custom_tracking_code' );
wp_footer (Action): Fires right before the closing </body> tag on the frontend. Ideal for placing deferred JavaScript files or footer analytics.
add_action( 'wp_footer', 'my_custom_footer_scripts' );
Initialization & System Hooks
init (Action): Runs after WordPress finishes loading core components, but before headers are sent. Perfect for registering custom post types, taxonomies, or session logic.
add_action( 'init', 'my_custom_post_type_init' );
wp_loaded (Action): Triggers after the WordPress core execution and active plugins are fully loaded. Useful for execution logic that depends on all plugins being active.
add_action( 'wp_loaded', 'my_custom_process_submission' );
Script & Style Enqueueing Hooks
wp_enqueue_scripts (Action): The standard hook for registering and enqueueing stylesheets and JavaScript files for the frontend.
add_action( 'wp_enqueue_scripts', 'my_frontend_assets' );
Content & Redirect Hooks
save_post (Action): Triggers whenever a post, page, or custom post type is saved or updated in the database.
add_action( 'save_post', 'my_save_post_meta_data' );
template_redirect (Action): Executes right before WordPress decides which template file to load. Frequently used for custom page redirects based on URL criteria.
add_action( 'template_redirect', 'my_custom_page_redirect' );
login_redirect (Filter): Intercepts and alters the destination URL after a user logs into WordPress.
add_filter( 'login_redirect', 'my_custom_login_redirect', 10, 3 );
How to Hook and Unhook Functions in WordPress
1. Hooking into an Action
To register an action hook, pass your hook name, callback function name, priority, and accepted arguments:
add_action( $hook_name, $callback_function, $priority, $accepted_args );
$hook_name(string, required): The name of the target action.$callback_function(callable, required): The custom function to run.$priority(int, optional): Order of execution (Default: 10). Lower numbers execute earlier; higher numbers execute later.$accepted_args(int, optional): Number of arguments passed to your callback (Default: 1).
2. Hooking into a Filter
Filters share a syntax structure identical to actions, but your callback function must return a value:
add_filter( $hook_name, $callback_function, $priority, $accepted_args );
1. What is an Action Hook?
An Action is triggered at a specific point during WordPress core initialization or rendering. Actions allow you to run custom PHP code—such as inserting metadata into a header or processing form entries—without altering the existing execution value.
2. What is a Filter Hook?
A Filter intercepts data or content before it is displayed on the screen or processed by the database. Filters receive a variable, allow you to modify that variable within your function, and require you to return the modified variable at the end.
3. Unhooking (Removing) Actions and Filters
To remove a hook attached by a core feature, parent theme, or third-party plugin, use remove_action() or remove_filter().
Important: The priority parameter must match the exact priority assigned when the original hook was registered.
// Remove an action hook
remove_action( 'wp_head', 'wp_generator' );
// Remove a filter hook
remove_filter( 'the_content', 'wpautop' );
Practical WordPress Hook Examples
Example 1: Register a Custom Admin Menu Page (Action Hook)
This code uses the admin_menu action hook to register a custom options page in the WordPress admin panel.
function my_custom_admin_menu() {
add_menu_page(
'Custom Settings', // Page Title
'Custom Menu', // Menu Title
'manage_options', // Capability Required
'my-custom-plugin-settings', // Menu Slug
'my_custom_menu_page_render',// Callback Function
'dashicons-admin-generic', // Dashicon Identifier
20 // Menu Position
);
}
add_action( 'admin_menu', 'my_custom_admin_menu' );
function my_custom_menu_page_render() {
echo '<div class="wrap"><h1>Custom Admin Settings Page</h1></div>';
}
Example 2: Modify Frontend Excerpt Length (Filter Hook)
This code uses the excerpt_length filter hook to change default post excerpts from 55 words down to 15 words.
function my_custom_excerpt_length( $length ) {
// Return custom word limit
return 15;
}
add_filter( 'excerpt_length', 'my_custom_excerpt_length', 999 );
Final Thoughts & Next Steps
WordPress hooks give you full control over your site’s functionality while keeping code clean, maintainable, and update-safe. By utilizing actions to execute tasks and filters to transform values, you can build custom theme features or standalone plugins effortlessly.
Looking to extend dynamic e-commerce features? Check out our complete guide on WooCommerce Pages Hooks to learn how to customize product, cart, and checkout layouts cleanly.
Need technical support or custom plugin development for your website? DevProvider, our sister company for developer work, places WordPress developers with your team.
Frequently asked questions
What is the difference between an action and a filter in WordPress?
An action performs a task at a specific point and does not return a value. A filter intercepts a value, modifies it and must return it. Actions are added with add_action() and filters with add_filter().
Which hook should I use to add CSS and JavaScript in WordPress?
Use the wp_enqueue_scripts action hook to register and enqueue stylesheets and scripts for the frontend.
Which hook registers custom post types?
The init action hook. It runs after WordPress has loaded its core components but before headers are sent, which is the right point to register custom post types and taxonomies.
How do I remove a hook added by a theme or plugin?
Call remove_action() or remove_filter() with the same hook name, callback and priority that were used to add it. If the priority does not match, the hook is not removed.
How do I change the excerpt length in WordPress?
Add a callback to the excerpt_length filter that returns the number of words you want, for example 15 instead of the default 55.