Answer: Hooks are points in WordPress code where themes and plugins can attach their own functions without editing core files. There are two types: action hooks and filter hooks.
- Action hooks let you do something at a specific moment, such as when the page header loads, a post is published or a user logs in. You attach a function with
add_action(); it does not need to return anything.
- Filter hooks let you change data before WordPress uses or displays it, such as a post's content, title or excerpt length. You attach a function with
add_filter(); it receives a value and must return the (modified) value.
// Action: add a line to the footer
add_action('wp_footer', function () {
echo '<p>Thanks for visiting!</p>';
});
// Filter: change excerpt length to 20 words
add_filter('excerpt_length', function ($length) {
return 20;
});
WordPress fires actions with do_action() and applies filters with apply_filters(); plugins can define their own hooks the same way.