Nearly every earlier lesson's functions.php code used add_action() — enqueuing assets, registering a menu, setting up theme support — without fully explaining the system underneath it. This lesson covers that system directly: WordPress's hooks, the mechanism that lets a theme (or plugin) run code at a specific point without editing WordPress's own core files.
| Hook type | Purpose | Registered with |
|---|---|---|
| Action | Run some code at a specific point — no return value expected | add_action() |
| Filter | Modify a piece of data and hand it back — always returns a value | add_filter() |
// functions.php
function wgh_setup_theme() {
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'wgh_setup_theme');after_setup_theme is the hook name — a specific moment WordPress reaches during every page load. add_action() attaches wgh_setup_theme to run at that exact moment.
Unlike an action, a filter's function always receives a value and must return one — often the same value, changed.
// Shorten every post excerpt to 15 words instead of the default 55
function wgh_shorter_excerpt($length) {
return 15;
}
add_filter('excerpt_length', 'wgh_shorter_excerpt');Themes and plugins don't just use WordPress's built-in hooks — they can define their own, letting other code (a child theme, a plugin) extend a specific point without editing the original file.
// Inside your theme's header.php, right after <body>
do_action('wgh_after_body_open');
// Anywhere else — a child theme's functions.php, for example —
// this now runs automatically at that exact point:
add_action('wgh_after_body_open', function () {
echo '<div class="announcement-bar">Free shipping this week!</div>';
});remove_action() and remove_filter() undo a hook — useful when a parent theme's behavior needs to be disabled from a child theme, covered in the next lesson.
remove_action('wp_head', 'wp_generator'); // hides the WordPress version number from the page sourceFinding the right hook name
A fast way to spot which hook to use for a given task: search the specific WordPress or plugin function's entry on developer.wordpress.org — its "Hooks" section lists every action and filter that fires inside it.