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.
- ✓Hooks are named points in WordPress's code where custom functions can attach.
- ✓They're the reason plugins and themes never need to edit WordPress core.
- ✓There are two kinds: actions and filters, each serving a different purpose.
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.
Runs custom code at a specific moment — sending an email, logging data, printing extra HTML. Doesn't return anything.
Takes a piece of data, lets your function modify it, and passes the modified version back. Must always return a value.
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
// 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
Why Hooks Exist in the First Place
Common Hook Mistakes
Breaking Down What You Just Learned
Hooks are extension pointsNamed moments in WordPress's code where custom functions can attach.
Actions do something extraThey run custom code at a specific point without returning a value.
Filters change dataThey receive a value, modify it, and must always return the result.
add_action() and add_filter()Both connect a hook name to your function name, the core syntax pattern.
Hooks replace core editsThey're why plugins and themes never need to modify WordPress's own files.
Try It Yourself
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
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.
- Write an action function that prints something extra using
wp_footeror a similar hook. - Write a filter function that modifies a value, like a post title or excerpt.
- Hook both into WordPress with add_action() and add_filter().
- Add both functions to your child theme's functions.php.
- Reload the site and confirm both changes appear correctly.
Recap
A named point in WordPress's code where custom functions can attach.
Runs custom code at a moment, without returning a value.
Receives, modifies, and returns a piece of data.
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.