Skip to content

PHP Tutorial PHP for WordPress Developers

WordPress Hooks

Lesson 329 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Hooks are WordPress’s main way of letting your code join in. Instead of editing WordPress core, you say “when this happens, call my function”.

If you remember Callback Functions, hooks are the same idea: WordPress keeps a list of callbacks for each named hook and calls them at the right time.

Two kinds

ActionFilter
Meaning“Something happened. Do something.”“Here is a value. Change it and give it back.”
Your functionDoes work (add a script, send an email)Returns a modified value
Registered withadd_action()add_filter()
WordPress callsdo_action()apply_filters()

A tiny model of how hooks work

This plain PHP version shows what happens behind the scenes.

index.php
PHP
<?php
$filters = [];

function my_add_filter(array &$filters, string $name, callable $fn): void
{
    $filters[$name][] = $fn;
}

function my_apply_filters(array $filters, string $name, mixed $value): mixed
{
    foreach ($filters[$name] ?? [] as $fn) {
        $value = $fn($value);
    }

    return $value;
}

my_add_filter($filters, "title", fn($t) => trim($t));
my_add_filter($filters, "title", fn($t) => strtoupper($t));

echo my_apply_filters($filters, "title", "  hello hooks  ");
Output
OUTPUT
HELLO HOOKS

WordPress stores callbacks in a list per hook name and runs them in order. The real functions add priorities, argument counts and more.

The real thing

index.php
PHP
<?php
add_filter("demo_title", fn($t) => trim($t));
add_filter("demo_title", fn($t) => strtoupper($t));

echo apply_filters("demo_title", "  hello hooks  ");
Output
OUTPUT
HELLO HOOKS

Hook names and where they fire

WordPress runs thousands of hooks. A few you will meet early:

HookTypeWhen
initactionWordPress has loaded, before output. Register post types here
wp_enqueue_scriptsactionThe place to add front-end CSS and JS
admin_menuactionAdd admin menu pages
save_postactionA post was saved
the_contentfilterThe post content is about to be printed
the_titlefilterA title is about to be printed
body_classfilterThe list of <body> classes

Priorities and arguments

index.php
PHP
<?php
add_action("demo_hook", function () { echo "second "; }, 20);
add_action("demo_hook", function () { echo "first "; }, 5);
add_action("demo_hook", function (string $name) { echo "hello $name"; }, 30, 1);

do_action("demo_hook", "reader");
Output
OUTPUT
first second hello reader

The third argument of add_action() is the priority (lower runs earlier, default 10). The fourth is how many arguments the callback accepts.

Callbacks: functions, closures and methods

index.php
PHP
<?php
add_action("init", "my_plugin_setup");                      // a function name
add_action("init", function () { /* ... */ });              // a closure
add_action("init", [$this, "setup"]);                       // an object method
add_action("init", [self::class, "staticMethod"]);          // a static method
add_action("init", $this->setup(...));                      // first-class callable (PHP 8.1+)

Closures cannot be removed later with remove_action(), because you have no name to give. Use named functions or methods when other code might need to unhook yours.

Remove a hook

index.php
PHP
<?php
function noisy(): void { echo "noisy "; }

add_action("demo_remove", "noisy");
remove_action("demo_remove", "noisy");

do_action("demo_remove");
echo has_action("demo_remove", "noisy") === false ? "removed" : "still there";
Output
OUTPUT
removed

Your own hooks

You can add hooks to your own plugin or theme, so other developers (or you later) can extend it. See Actions and Filters.

Why hooks matter

  • Updating WordPress does not erase your changes, because they live in your plugin or theme.
  • Many plugins can change the same thing, each adding its own callback.
  • Order is controlled with priorities.
info
This is the key idea of WordPress PHP
Once you understand “register a callback for a named moment”, most of WordPress development makes sense.