Understanding WordPress Hooks: Actions vs Filters

Hooks are the entire mechanism WordPress uses to let plugins and themes modify behavior without editing core files. Understanding the difference between actions and filters is the single most important WordPress development concept.

Actions: Do Something

// Runs a function at a specific point, without expecting a return value
add_action('wp_footer', function () {
    echo '<!-- Custom footer script here -->';
});

add_action('save_post', function ($post_id) {
    error_log("Post $post_id was saved");
});

Filters: Modify Something

// Receives a value, must return a (possibly modified) value
add_filter('the_title', function ($title) {
    return '★ ' . $title;
});

add_filter('excerpt_length', function ($length) {
    return 20; // shorter excerpts
});

The mental model: actions are for “when X happens, also do Y” (no return value needed), filters are for “here’s a value, give me back a modified version” (a return value is mandatory, or you’ll break whatever called the filter).