MODULE 03 · FUNCTIONALITY & CUSTOMIZATION

Hooks: Actions & Filters

Every add_action() and add_filter() you've written since Day 22 leans on the same underlying system. Today you'll understand what hooks actually are — the mechanism that lets themes and plugins plug custom code into WordPress without ever editing core files.

Day 24 of 30 Intermediate ~20 min

Plugging Into WordPress Without Editing It

WordPress core, every theme, and every plugin need a way to let other code step in at exactly the right moment — before a post saves, after the page title renders, right before the footer loads. Hooks are that mechanism. They're specific, named points scattered throughout WordPress where custom code can attach itself and run.

This is exactly why core files should never be edited directly: hooks give you every extension point you need without touching a single line of WordPress itself.

Two Kinds of Hook

Every hook in WordPress falls into one of these two categories, and the difference matters for how you write your function.

HOOK TYPE WHAT IT DOES
Action

Runs custom code at a specific moment — sending an email, logging data, printing extra HTML. Doesn't return anything.

Filter

Takes a piece of data, lets your function modify it, and passes the modified version back. Must always return a value.

THINK OF IT AS

Actions are for "do something extra here." Filters are for "change this value before WordPress uses it."

An Action and a Filter Side by Side

functions.php
// Action — runs code, returns nothing
add_action( 'wp_footer', 'my_footer_note' );
function my_footer_note() {
    echo '<p>Built with WordPress</p>';
}

// Filter — modifies a value and returns it
add_filter( 'the_title', 'my_title_prefix' );
function my_title_prefix( $title ) {
    return '★ ' . $title;
}

Both follow the same pattern — hook name, function name — but a filter always receives and returns a value.

Attaching Custom Code to a Hook

  • Decide what you want to happen and roughly when — that points you toward action or filter.
  • Find the right hook name by searching the WordPress Developer Reference for the moment you need.
  • Write a function that does the work — printing HTML for an action, or modifying and returning data for a filter.
  • Call add_action() or add_filter(), passing the hook name and your function's name.
  • Add this code to your child theme's functions.php, never the parent theme.
  • Reload the site to confirm the change took effect where expected.
  • Why Hooks Exist in the First Place

    EDITING CORE FILES Changing WordPress's own files directly means every update overwrites the change — and updates become genuinely risky to run.
    HOOKING IN INSTEAD Attaching to a hook from your own theme or plugin means core updates never touch your code, and your changes survive indefinitely.

    Common Hook Mistakes

    WATCH FOR THESE
    FORGETTING TO RETURN A filter function that echoes instead of returning a value will break the data it was meant to modify.
    WRONG HOOK NAME A misspelled or outdated hook name means the function silently never runs, with no error shown.
    PRIORITY MATTERS When multiple functions hook into the same place, the optional priority argument controls what order they run in.

    Breaking Down What You Just Learned

    1

    Hooks are extension pointsNamed moments in WordPress's code where custom functions can attach.

    2

    Actions do something extraThey run custom code at a specific point without returning a value.

    3

    Filters change dataThey receive a value, modify it, and must always return the result.

    4

    add_action() and add_filter()Both connect a hook name to your function name, the core syntax pattern.

    5

    Hooks replace core editsThey're why plugins and themes never need to modify WordPress's own files.

    Try It Yourself

    EXERCISE

    Write a filter function hooked to the_content that appends a short "Thanks for reading" message to the end of every post's content.

    Add One Action and One Filter

    HANDS-ON EXERCISE

    Putting both hook types to work on your capstone site

    By the end of today, your child theme should include one working action and one working filter that you wrote yourself.

    1. Write an action function that prints something extra using wp_footer or a similar hook.
    2. Write a filter function that modifies a value, like a post title or excerpt.
    3. Hook both into WordPress with add_action() and add_filter().
    4. Add both functions to your child theme's functions.php.
    5. Reload the site and confirm both changes appear correctly.

    Recap

    HOOK

    A named point in WordPress's code where custom functions can attach.

    ACTION

    Runs custom code at a moment, without returning a value.

    FILTER

    Receives, modifies, and returns a piece of data.

    PRIORITY

    An optional argument controlling the order multiple hooked functions run in.

    Key Takeaways

    • Hooks are named extension points scattered throughout WordPress's code.
    • Actions run custom code at a specific moment; filters modify and return a value.
    • add_action() and add_filter() both connect a hook name to a custom function.
    • A filter function that doesn't return a value will break whatever it was meant to modify.
    • Hooks are exactly why themes and plugins never need to edit WordPress core files.
    Course Overview