If you want to build professional WordPress plugins—or customize WordPress without touching core files—you must understand hooks. Hooks are the foundation of plugin development and one of the most powerful parts of the WordPress ecosystem.
They allow developers to “hook into” WordPress at specific moments and run custom code without modifying WordPress itself.
This means your plugin or theme stays secure, update-safe, and fully compatible with the platform.
In this complete guide, we’ll break down:
- What hooks are
- The difference between actions and filters
- When to use each type
- How priority and parameters work
- Real code examples
- Tips and best practices
- How hooks interact with plugins and themes
- How to create your own hooks
By the end, you’ll understand hooks clearly enough to build real-world plugins and extend WordPress safely and efficiently.
What Are WordPress Hooks?
Hooks allow your custom code to interact with WordPress during specific events.
For example:
- When a post is saved
- When a page loads
- When WordPress outputs content
- When an admin screen is displayed
- When updating settings
- When printing scripts
- When sending emails
- When loading plugins
Hooks make it possible to:
✔ Add new features
✔ Modify existing behavior
✔ Extend plugins and themes
✔ Build commercial products
✔ Remove unwanted defaults
You will use hooks in every plugin you ever build.
Two Types of Hooks
WordPress has two major hook types:
1. Action Hooks
Used to add or run something.
Examples:
- Add custom content
- Insert a script
- Save data
- Create admin menus
- Trigger background tasks
Syntax:
add_action('hook_name', 'your_function', $priority, $accepted_args);
2. Filter Hooks
Used to modify data before it is displayed or processed.
Examples:
- Change titles
- Modify excerpts
- Edit menu items
- Filter content before display
- Modify emails
Syntax:
add_filter('hook_name', 'your_function', $priority, $accepted_args);
If you remember only one rule:
👉 Actions do something.
Filters change something.
Understanding Priority and Arguments
Most hooks allow you to set:
Priority
Defines the order in which functions execute.
Example:
add_action('wp_footer', 'my_first_function', 5);
add_action('wp_footer', 'my_second_function', 15);
Lower number = earlier execution.
Accepted Arguments
Hooks can pass data into your function.
Example:
add_filter('the_title', 'modify_title', 10, 2);
function modify_title($title, $post_id) {
return 'Updated: ' . $title;
}
The 2 means we expect two parameters.
Real Action Hook Examples
Example 1: Add a message to the footer
add_action('wp_footer', function() {
echo '<p style="text-align:center;">Powered by My Custom Plugin</p>';
});
Example 2: Add a custom admin menu
function mfp_add_admin_menu() {
add_menu_page(
'My Plugin Settings',
'My Plugin',
'manage_options',
'mfp-settings',
'mfp_settings_page_html'
);
}
add_action('admin_menu', 'mfp_add_admin_menu');
Example 3: Run code when a post is saved
function mfp_save_post($post_id) {
// your logic here
}
add_action('save_post', 'mfp_save_post');
When to use:
Use action hooks any time you want to execute code.
Real Filter Hook Examples
Example 1: Modify post titles
function mfp_modify_title($title) {
return '🔥 ' . $title;
}
add_filter('the_title', 'mfp_modify_title');
Example 2: Change excerpt length
function mfp_excerpt_length() {
return 25;
}
add_filter('excerpt_length', 'mfp_excerpt_length');
Example 3: Add a message at the bottom of content
function mfp_add_content_message($content) {
if (is_single()) {
$content .= '<p>Thanks for reading!</p>';
}
return $content;
}
add_filter('the_content', 'mfp_add_content_message');
When to use:
Use filters any time you want to modify data.
Where Do Hooks Come From?
Hooks are defined by:
- The WordPress core
- Themes
- Plugins
- Your own plugins
For example:
WordPress emits hooks during events:
do_action('init');
do_action('wp_footer');
apply_filters('the_title', $title);
Themes output hooks to allow customization:
<?php do_action('after_header'); ?>
Plugins expose their own hooks so other developers can extend their functionality.
How to Discover Hooks
There are thousands of hooks available.
You can find them:
1. In WordPress Developer Reference
2. Using a plugin like Query Monitor
Enables real-time inspection of triggered hooks.
3. Searching the WordPress core directory
Look for:
do_action()apply_filters()
4. Reading plugin/theme documentation
When building professional plugins, reading other plugins’ hook architecture teaches you best practices.
How to Create Your Own Hooks
This is the key to building professional-grade plugins.
Create an action hook
do_action('mfp_after_saving');
Create a filter hook
$value = apply_filters('mfp_modify_value', $value);
When your plugin uses custom hooks, other developers can extend it—this is how large plugins like WooCommerce grow their ecosystem.
Real Example: Custom Hooks in Your Plugin
Let’s say you want to let other developers modify a message.
function mfp_show_message() {
$message = 'Welcome to my plugin!';
$message = apply_filters('mfp_message', $message);
echo esc_html($message);
}
Now other plugins can do:
add_filter('mfp_message', function($text) {
return '🔥 ' . $text;
});
Congratulations — you just made a plugin extensible.
Commonly Used WordPress Hooks
Here are some of the most important hooks you’ll use repeatedly.
Initialization Hooks
init
Runs early, great for:
- Custom post types
- Shortcodes
- Taxonomies
add_action('init', 'mfp_register_features');
Frontend Hooks
wp_head
Insert scripts in the page <head>.
add_action('wp_head', 'mfp_meta_tags');
wp_footer
Inject content before closing </body>.
Content Hooks
the_content
Modify post content.
the_title
Change post titles.
Admin Hooks
admin_menu
Add settings pages.
admin_init
Register settings, fields, and validation.
Saving & Updating Hooks
save_post
When a post is saved or updated.
user_register
When a new account is created.
E-Commerce Hooks (WooCommerce)
woocommerce_before_cart
Add notices or extra scripts above the cart.
woocommerce_single_product_summary
Insert content in product pages.
Hook Best Practices for Developers
Using hooks properly takes skill. Here are essential best practices:
1. Always Use Meaningful Function Names
Good:
add_action('init', 'mfp_register_custom_post_types');
Bad:
add_action('init', 'register_stuff');
2. Always Escape Output
Never trust user input:
echo esc_html($message);
3. Always Sanitize Input
$message = sanitize_text_field($_POST['message']);
4. Comment Hook Usage Clearly
Future developers will thank you.
5. Avoid Anonymous Functions for Public Plugins
While closures are convenient, they cannot be removed easily:
remove_action( ... ) // cannot remove closure
Always use named functions for public code.
6. Use Priorities Carefully
Higher priority numbers run later.
Use this to override other plugins if needed.
7. Don’t Overuse Hooks
Hooks are powerful—but too many hooks can make debugging difficult.
Debugging Hooks
Enable WP_DEBUG
define('WP_DEBUG', true);
Use Query Monitor Plugin
Shows:
- Which hooks fired
- In what order
- Their arguments
- Execution times
Log Hook Output
error_log('Hook triggered');
Putting It All Together: A Real Plugin Example
Let’s build a simple plugin using multiple hooks.
Plugin Output
- Adds an admin page
- Saves settings
- Displays custom footer message
- Allows other developers to modify message via filters
Code
<?php
/**
* Plugin Name: Custom Footer Message
*/
function cfm_add_admin_menu() {
add_options_page(
'Footer Message Settings',
'Footer Message',
'manage_options',
'cfm-settings',
'cfm_settings_page_html'
);
}
add_action('admin_menu', 'cfm_add_admin_menu');
function cfm_settings_page_html() {
?>
<div class="wrap">
<h1>Footer Message Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('cfm_settings_group');
do_settings_sections('cfm-settings');
submit_button();
?>
</form>
</div>
<?php
}
function cfm_register_settings() {
register_setting('cfm_settings_group', 'cfm_message');
}
add_action('admin_init', 'cfm_register_settings');
function cfm_footer_message() {
$message = get_option('cfm_message', 'Thank you for visiting!');
$message = apply_filters('cfm_modify_message', $message);
echo '<p style="text-align:center;">' . esc_html($message) . '</p>';
}
add_action('wp_footer', 'cfm_footer_message');
This example uses:
- Action hooks: admin_menu, admin_init, wp_footer
- Filter hook: cfm_modify_message
- Core functions: register_setting, add_options_page
This is the foundation of real plugin development.
Conclusion
Hooks are one of the most powerful concepts in WordPress. They give developers full control over how and when their code interacts with the platform—without modifying WordPress core.
By understanding actions and filters, you can:
- Create plugins
- Customize themes
- Modify output
- Add features
- Make your code modular
- Build products for clients
- Create commercial plugins
- Extend other plugins (like WooCommerce)**
Hooks are essential to becoming a professional WordPress developer.
If you master hooks, you master WordPress.

