WordPress Hooks and Filters

WordPress Hooks Explained: Actions vs Filters

How WordPress hooks work, the real difference between actions and filters, and how to use add_action and add_filter with priorities, arguments and working examples.

Diagram of WordPress core firing an action hook and passing a value through a filter hook to callback functions

WordPress hooks are named points in the code where WordPress lets you run your own functions. There are two kinds: actions, which let you do something at a given moment (send an email, add a script, save data), and filters, which let you change a value before WordPress uses it (a title, a query, the HTML of a block). Every theme and plugin is built on hooks, and they are the reason you can change almost anything in WordPress without editing core files.

This guide explains how the hook system works under the hood, when to use an action or a filter, how priority and arguments behave, and the mistakes that cause most "my hook doesn't run" problems.

How the hook system works

WordPress keeps a global registry of hooks in $wp_filter. Each entry is a WP_Hook object that holds a list of callbacks sorted by priority. Two things happen:

  1. Registration. Your code calls add_action() or add_filter(), which adds your callback to the list for that hook name.
  2. Execution. When WordPress (or a plugin) reaches the hook in its own code, it calls do_action() or apply_filters(), and every registered callback runs in priority order.

Internally, actions and filters share the same storage. add_action() is literally a wrapper around add_filter(). The difference is in how they are called and what is expected of your callback.

Actions vs filters

ActionFilter
PurposeDo something at a point in timeChange a value and hand it back
Fired withdo_action( 'name', $args... )apply_filters( 'name', $value, $args... )
Registered withadd_action()add_filter()
Your callback must returnNothing (return value ignored)The value, changed or not
Typical examplesinit, wp_enqueue_scripts, save_postthe_content, the_title, wp_mail_from

A simple rule: if you want to add or trigger something, use an action. If you want to modify something WordPress is about to output or store, use a filter.

Using an action: a working example

This adds a stylesheet on the front end. The wp_enqueue_scripts action fires at the right time to register assets.

add_action( 'wp_enqueue_scripts', 'tle_enqueue_assets' );

function tle_enqueue_assets() {
    wp_enqueue_style(
        'tle-main',
        get_stylesheet_directory_uri() . '/assets/main.css',
        array(),
        '1.0.0'
    );
}

The callback returns nothing. It just does its job when the action fires.

Using a filter: a working example

This appends a short note to the end of single blog posts. The the_content filter passes the post HTML in, and you must return it.

add_filter( 'the_content', 'tle_append_note' );

function tle_append_note( $content ) {
    if ( is_singular( 'post' ) && in_the_loop() && is_main_query() ) {
        $content .= '<p class="post-note">Questions? Leave a comment below.</p>';
    }
    return $content; // always return, even when you change nothing
}

Forgetting the return is the single most common filter bug: the content disappears because your function returned null.

Priority: controlling the order

The third parameter of add_action() and add_filter() is the priority. Lower numbers run first; the default is 10. Callbacks with the same priority run in the order they were added.

add_filter( 'the_title', 'tle_first', 5 );   // runs before most others
add_filter( 'the_title', 'tle_last', 99 );   // runs after most others

Use a late priority (for example 20 or 99) when you need to override what another plugin did, and an early one when you want to prepare a value before others see it.

Accepted arguments

The fourth parameter tells WordPress how many arguments your callback wants. The default is 1, so extra arguments are not passed unless you ask for them.

// save_post passes $post_id, $post and $update
add_action( 'save_post', 'tle_on_save', 10, 3 );

function tle_on_save( $post_id, $post, $update ) {
    if ( wp_is_post_revision( $post_id ) || ! $update ) {
        return;
    }
    // ... your logic
}

If you declare three parameters but leave the fourth argument at 1, PHP throws an ArgumentCountError for the missing arguments.

Callbacks: functions, methods and closures

A callback can be any PHP callable:

  • a function name: 'tle_on_save'
  • a static method: array( 'TLE_Plugin', 'boot' ) or 'TLE_Plugin::boot'
  • an object method: array( $this, 'enqueue' )
  • a closure: function () { ... }

Closures are convenient, but they cannot easily be removed later by other code, because there is no name to reference. For anything a child theme or another plugin might want to unhook, use a named function or a method on an object they can reach.

Removing a hook

remove_action() and remove_filter() only work when you pass the same hook name, callback and priority used when it was added, and only after it was added.

add_action( 'init', function () {
    remove_action( 'wp_head', 'wp_generator' ); // core adds this at priority 10
} );

We cover the edge cases (class methods, anonymous objects, timing) in how to remove actions and filters correctly.

Creating your own hooks

Hooks are not just for consuming. Adding them to your own theme or plugin lets others extend it without editing your files:

$price = apply_filters( 'tle_course_price', $price, $course_id );
do_action( 'tle_after_enrollment', $user_id, $course_id );

Prefix hook names with your own namespace so they never clash. See how to create custom hooks.

Common mistakes and how to fix them

  • Hooking too late. Adding an action to init from inside a wp_head callback means init has already fired. Check the hook execution order.
  • Filter returns nothing. Always return the first argument.
  • Wrong number of arguments. Set the fourth parameter to match your function signature.
  • Running everywhere. A the_content filter also runs in feeds, excerpts and REST responses. Use conditionals like is_main_query() and in_the_loop().
  • Removing with the wrong priority. remove_action() silently fails if the priority does not match.
  • Heavy work on frequent hooks. Hooks like gettext or option_* fire hundreds of times per request; keep callbacks cheap.

How to see which hooks run

The free Query Monitor plugin lists the hooks fired on a request and the callbacks attached to each. For a specific hook you can inspect the registry directly:

global $wp_filter;
error_log( print_r( array_keys( $wp_filter['the_content']->callbacks ), true ) );

More techniques in how to find which hooks run on a page.

Frequently asked questions

What is the difference between an action and a filter in WordPress?

An action runs your code at a point in time and ignores the return value. A filter passes a value to your code and uses whatever you return. Use actions to do things and filters to change things.

What is the default hook priority?

The default priority is 10. Lower numbers run earlier, higher numbers later, and callbacks with equal priority run in the order they were added.

Can I use add_action for a filter hook?

Technically yes, because both use the same registry, but your callback must still return the value or the output breaks. Use add_filter() for filters so the intent is clear.

Where should I put my hook code?

Site-specific functionality belongs in a small custom plugin or a must-use plugin; presentation tweaks tied to one theme belong in a child theme's functions.php.

Conclusion

Once you understand that every hook is a named list of callbacks run in priority order, WordPress becomes predictable: actions to act, filters to change and return, priority to order, and accepted arguments to receive data. From here, explore add_action in depth, add_filter with practical examples and WooCommerce hooks. If you work with Tutor LMS, the same principles apply to its action and filter hooks.

Further reading: the official Plugin Handbook on hooks and the WP_Hook class reference.

Need a customization built on hooks so it survives every update? Talk to our developers.

Save this practical guide

Add us as a preferred Google source, or ask an AI assistant to summarize this page and link back to the original guide when it is useful.

· Tutor LMS developers

Guides written and reviewed by the TutorLMS Expert development team. Every hook, function, table and setting we mention is checked against the Tutor LMS and Tutor LMS Pro source code before it is published.

Keep reading

Need a custom Tutor LMS solution?

Tell us what you are building or what is broken. A Tutor LMS developer reads every inquiry and replies with next steps and a free estimate.

Get a free Tutor LMS consultation

Tell us what you are building or fixing. A Tutor LMS developer replies with honest advice and next steps.

  • No obligation
  • Tutor LMS and Pro specialists
  • Your details stay private

This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.