How to Create Custom Hook in WordPress: Simple Guide
In WordPress, you create a custom hook by defining an
action or filter using do_action('hook_name') or apply_filters('hook_name', $value). Then, other code can attach functions to your hook using add_action or add_filter to run custom code at that point.Syntax
WordPress custom hooks come in two types: actions and filters. You create them by calling specific functions in your code.
do_action('hook_name', $arg1, $arg2, ...): Runs all functions attached to this action hook.apply_filters('hook_name', $value, $arg1, $arg2, ...): Passes a value through all functions attached to this filter hook and returns the modified value.
Other code uses add_action('hook_name', 'function_name') or add_filter('hook_name', 'function_name') to connect functions to your custom hook.
php
<?php // Creating a custom action hook function my_custom_function() { do_action('my_custom_action', 'Hello World'); } // Creating a custom filter hook function my_custom_filter($content) { return apply_filters('my_custom_filter', $content); } ?>
Example
This example shows how to create a custom action hook and attach a function to it that prints a message.
php
<?php // Define the custom action hook function greet_user() { do_action('say_hello', 'Alice'); } // Attach a function to the custom hook add_action('say_hello', function($name) { echo "Hello, $name! Welcome to WordPress."; }); // Call the function that triggers the hook // Output will appear where this function is called greet_user(); ?>
Output
Hello, Alice! Welcome to WordPress.
Common Pitfalls
Common mistakes when creating custom hooks include:
- Not calling
do_actionorapply_filtersin your code, so the hook never runs. - Using inconsistent hook names, which causes attached functions not to run.
- For filters, forgetting to return the modified value inside the hooked function.
- Attaching functions to hooks before the hook is defined or available.
php
<?php // Wrong: filter function does not return value add_filter('my_filter', function($text) { $text .= ' added text'; // Missing return statement }); // Right: filter function returns modified value add_filter('my_filter', function($text) { $text .= ' added text'; return $text; }); ?>
Quick Reference
| Function | Purpose | Usage Example |
|---|---|---|
| do_action | Runs all functions attached to an action hook | do_action('hook_name', $arg1) |
| apply_filters | Passes a value through filter functions and returns it | $value = apply_filters('hook_name', $value) |
| add_action | Attaches a function to an action hook | add_action('hook_name', 'function_name') |
| add_filter | Attaches a function to a filter hook | add_filter('hook_name', 'function_name') |
Key Takeaways
Create custom hooks with do_action for actions and apply_filters for filters.
Attach functions to hooks using add_action or add_filter to extend functionality.
Always return the modified value in filter functions to avoid bugs.
Use consistent and unique hook names to prevent conflicts.
Call your custom hook functions where you want the hooked code to run.