WordPress Hooks: Actions and Filters
Hooks are the most important idea in WordPress development. A hook is a place in the WordPress code where you can run your own function, or change some data, without editing the core files. Themes and plugins both use hooks. There are two types: actions and filters.
| Point | Action | Filter |
|---|---|---|
| Purpose | Do something at a moment | Change a value and give it back |
| Add with | add_action() | add_filter() |
| Run by | do_action() | apply_filters() |
| Return value | Not needed | Must return the (changed) value |
Actions
An action runs your function when WordPress reaches that moment. Here the function is attached to a hook which we create ourselves with do_action():
function tc_say_hello( $name ) {
echo "Hello, $name!\n";
}
add_action( 'tc_greeting', 'tc_say_hello' );
do_action( 'tc_greeting', 'Riya' );
echo 'Times the action ran: ' . did_action( 'tc_greeting' );
Hello, Riya!
Times the action ran: 1
Filters
A filter receives a value, changes it and returns it. Always return the value, even if you did not change it, otherwise the content will disappear.
function tc_add_signature( $content ) {
return $content . ' -- Team Tutorials Class';
}
add_filter( 'tc_message', 'tc_add_signature' );
echo apply_filters( 'tc_message', 'Thanks for reading' );
Thanks for reading — Team Tutorials Class
Priority and accepted arguments
add_action( $hook, $callback, $priority = 10, $accepted_args = 1 ). A lower priority number runs earlier. The default is 10.
add_action( 'tc_steps', function () { echo "B (priority 10)\n"; } );
add_action( 'tc_steps', function () { echo "A (priority 5)\n"; }, 5 );
add_action( 'tc_steps', function () { echo "C (priority 20)\n"; }, 20 );
do_action( 'tc_steps' );
A (priority 5)
B (priority 10)
C (priority 20)
If the hook passes more than one value, say how many your function wants as the fourth parameter:
add_filter( 'tc_price', function ( $price, $tax_percent ) {
return $price + ( $price * $tax_percent / 100 );
}, 10, 2 );
echo apply_filters( 'tc_price', 200, 18 );
236
Remove a hook
function tc_noisy() { echo "noisy\n"; }
add_action( 'tc_event', 'tc_noisy' );
remove_action( 'tc_event', 'tc_noisy' );
do_action( 'tc_event' );
echo has_action( 'tc_event', 'tc_noisy' ) ? 'still attached' : 'removed';
removed
Real WordPress hooks
| Hook | Type | When it runs |
|---|---|---|
| init | action | After WordPress is loaded. Register post types and taxonomies here |
| after_setup_theme | action | Theme setup, add_theme_support |
| wp_enqueue_scripts | action | Load CSS and JS on the front |
| wp_head, wp_footer | action | Inside the head and before the closing body tag |
| widgets_init | action | Register sidebars and widgets |
| save_post | action | After a post is saved |
| admin_menu | action | Add pages to the dashboard menu |
| the_content | filter | Change the content of a post before it is shown |
| the_title | filter | Change post titles |
| body_class | filter | Add classes to the body tag |
A real filter on the_content
$ curl -s http://example.com/hello-world/ | grep "Thanks for reading"
<p><em>Thanks for reading this post.</em></p></article>
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |