WordPress Hooks

Beginner
7 min read
Updated: Nov 2026
2 examples
Core concept

Definition

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

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' );

Actions vs Filters

They look alike, but they have different jobs. This table shows the difference.

ActionFilter
Idea“When this happens, run my code.”“Let me change this value.”
Attach your functionadd_action()add_filter()
WordPress triggers it withdo_action()apply_filters()
Your function must returnNothing. Any return value is ignored.The value. Always use return.
Remove it withremove_action()remove_filter()
Popular hookwp_enqueue_scripts, init, wp_headthe_content, the_title, excerpt_length

Priority: when many functions use the same hook, the one with the lower number runs first. The default is 10.

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?”

  1. 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.
  2. 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).
  3. Priority = the line. Helpers with lower numbers stand earlier in line.
  4. 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.

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.”

WordPress
function ctf_load_styles() {
  wp_enqueue_style( 'ctf-main', get_stylesheet_uri(), array(), '1.0' );
}
add_action( 'wp_enqueue_scripts', 'ctf_load_styles' );

How It Works

  1. wp_enqueue_scripts is an action hook. WordPress fires it when it prepares the styles and scripts.
  2. add_action() attaches ctf_load_styles() to it, so it runs at that moment.
  3. The function does a job (adds a stylesheet) and returns nothing.

Example 2 — Change the excerpt length with a filter

A filter: “when WordPress asks for the excerpt length, let me change it.”

WordPress
function ctf_excerpt_length( $length ) {
  return 20;
}
add_filter( 'excerpt_length', 'ctf_excerpt_length' );

How It Works

  1. excerpt_length is a filter hook. WordPress passes in the default value (55) as $length.
  2. add_filter() attaches ctf_excerpt_length() to it.
  3. The function returns 20, and WordPress uses that number. You can filter the_content in the same way.

Next: WordPress Hooks Functions Index

Now that you know actions and filters, open the functions index for all 6 guides.

Full list: WordPress Hooks Functions index (/wordpress/hooks/function).

Frequently Asked Questions

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.

Browse every hooks function

Open the functions index for simple guides with examples and FAQs.

Hooks Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful