One of the most powerful features of WordPress is its hook system, which allows developers to modify or extend core functionality without touching core files.
If you’re developing custom themes or plugins, understanding WordPress hooks actions and filters is essential. This guide will break down what hooks are, how to use them, and provide real-world code examples to help you write clean, extendable, and future-proof code.
What Are WordPress Hooks?
In WordPress, hooks are places in the core code that allow you to “hook into” certain processes at specific times. They let you:
- Run your own functions at a specific point (using actions)
- Modify data before it’s rendered or saved (using filters)
Types of WordPress Hooks :
1. Action Hooks
Let you add functionality at specific execution points.
Example: Sending an email after a post is published.
2. Filter Hooks
Let you modify data before it is used or displayed.
Example: Changing the content of a post before it is shown.
Basic Syntax
Action Hook Example
function my_custom_function() {
// Do something
error_log('A post was published!');
}
add_action('publish_post', 'my_custom_function');
This will run my_custom_function()
every time a post is published.
Filter Hook Example
function modify_post_title($title) {
if (is_admin()) return $title;
return 'Title: ' . $title;
}
add_filter('the_title', 'modify_post_title');
This prepends a fire emoji to every post title on the front end.
Where to Place Hooks
- Add action/filter hooks in your:
- functions.php (for themes)
- custom plugin file
- Never edit WordPress core files
Commonly Used Hooks
Action Hooks
Hook Name | Purpose |
---|---|
init | Runs after WordPress is loaded |
wp_enqueue_scripts | Enqueue CSS/JS files |
save_post | Triggered when a post is saved |
admin_menu | Add admin pages |
Filter Hooks
Hook Name | Purpose |
---|---|
the_content | Filter post content |
the_title | Filter post title |
excerpt_more | Customize “read more” link |
upload_mimes | Allow new file types in uploads |
Real-World Examples :
1. Add Custom Class to Body Tag
function add_custom_body_class($classes) {
$classes[] = 'my-custom-class';
return $classes;
}
add_filter('body_class', 'add_custom_body_class');
2. Send Email on New User Registration
function notify_admin_new_user($user_id) {
$user_info = get_userdata($user_id);
wp_mail('admin@example.com', 'New User', 'A new user registered: ' . $user_info->user_email);
}
add_action('user_register', 'notify_admin_new_user');
Debugging & Best Practices
Use do_action()
and apply_filters()
in Your Code
- do_action(‘your_custom_hook’) — for triggering actions
- apply_filters(‘your_filter_hook’, $value) — for allowing filters
Use add_action()
and add_filter()
responsibly
- Avoid function name conflicts (use prefixes)
- Use anonymous functions only when you don’t need to remove the hook later
- Use priority values if your hook depends on execution order
add_action('init', 'my_init_function', 15); // Lower = earlier
Remove or Modify Existing Hooks
Remove Default Function
remove_filter('the_content', 'wpautop');
Modify WooCommerce Product Title
function change_product_title($title, $id = null) {
if (!is_admin() && $id && get_post_type($id) === 'product') {
return 'Product: ' . $title; // You can change the prefix as needed
}
return $title;
}
add_filter('the_title', 'change_product_title', 10, 2);
Using Hooks Safely
- Validate all inputs
- Sanitize outputs (
sanitize_text_field()
,esc_html()
, etc.) - Add
priority
andaccepted_args
parameters for control - Use conditionals like
is_admin()
oris_single()
to limit scope
Conclusion
Hooks are what make WordPress flexible and extendable. Whether you’re developing themes, building plugins, or customizing your site, learning how to use action and filter hooks is a must.
Start by using built-in hooks, then create your own for more advanced, modular, and maintainable code.