A hook is a named spot in WordPress where you can plug in your own code. You add your code from functions.php or a plugin, so you never edit WordPress core files.
There are two kinds of hooks: actions, which let you do something, and filters, which let you change a value. Hooks are a core idea in WordPress. Almost every theme and plugin uses them.
Where to use them.
Write hook code in your theme's functions.php or in a plugin file. Popular hooks are init, wp_enqueue_scripts, wp_head, and the_content.
Syntax
Syntax
Both kinds follow the same pattern: name the hook, name your function.
WordPress
<?php
// Action: do something at a moment.
add_action( 'hook_name', 'my_action_function' );
// Filter: change a value, then return it.
add_filter( 'hook_name', 'my_filter_function' );
Reference
Actions vs Filters
They look alike, but they have different jobs. This table shows the difference.
Priority: when many functions use the same hook, the one with the lower number runs first. The default is 10.
Think
A Simple Mental Model
Picture WordPress as a long set of steps. At some steps it stops and asks, “Does anyone want to add something here?”
Action = a doorbell. When it rings (do_action()), everyone who signed up (add_action()) comes and does their job. Nobody has to hand anything back.
Filter = a conveyor belt. A value rides the belt (apply_filters()). Each helper you added (add_filter()) may change it and must put it back on the belt (return).
Priority = the line. Helpers with lower numbers stand earlier in line.
Remove = leave the line.remove_action() and remove_filter() take a helper out.
Do not forget.
A filter function that forgets return breaks the value. The post content, title, or setting becomes empty.
Code
WordPress Examples
One action and one filter, with sample output.
Example 1 — Load a stylesheet with an action
An action: “when WordPress loads styles, run my function.”
A hook is a named point in WordPress code. At that point you can run your own code (an action) or change a value (a filter) without editing core files.
Actions let you do something, such as print HTML or load a file, and return nothing. Filters let you change a value, and your function must return it.
Write it in your theme's functions.php or in a plugin. Do not edit WordPress core files, because updates would erase your changes.
Your filter function probably forgot to return the value. A filter must always return the value it received, even if it did not change it.
Priority is the order of running when several functions use the same hook. Lower numbers run earlier. The default is 10.
Open the WordPress Hooks Functions index. It lists all six guides with short descriptions.