Hooks, Actions & Filters

45-60 minIntermediate

The WordPress Hook System is how plugins and themes extend WordPress functionality without modifying core files. Hooks are predefined points where you can inject your own code. Actions let you DO something; Filters let you CHANGE something. Master hooks and you master WordPress development.

Prerequisites

  • Basic PHP knowledge (functions, parameters)
  • Access to functions.php or a custom plugin
  • Understanding of WordPress execution flow

Step-by-Step Guide

1

Understand the Two Types of Hooks

1

Actions: Goal is to DO something at a specific point in time

2

Actions add functionality, echo output, register resources

3

Examples: send email after post published, add menu item, load scripts

4

Filters: Goal is to CHANGE something before it is used

5

Filters modify text, variables, arrays, or objects and return them

6

Examples: modify post content, change excerpt length, filter titles

Ask yourself: Am I doing something new? (Action) Or changing something existing? (Filter)
2

Master Action Hooks

1

add_action() attaches your function to a WordPress action hook

2

Syntax: add_action( $hook_name, $callback_function, $priority, $accepted_args )

3

Priority (default 10): Lower numbers run earlier

4

Common hooks: init, wp_enqueue_scripts, admin_menu, wp_footer

Example
// Example: Add custom JavaScript to the front-end
add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );
function my_custom_scripts() {
    wp_enqueue_script( 
        'my-script', 
        get_stylesheet_directory_uri() . '/js/custom.js',
        array( 'jquery' ),
        '1.0',
        true  // Load in footer
    );
}

// Example: Run code when WordPress initializes
add_action( 'init', 'my_init_function' );
function my_init_function() {
    // Register custom post types here
    // This runs after WordPress loads but before headers are sent
}
3

Master Filter Hooks

1

add_filter() attaches your function to modify data

2

Your function receives data, modifies it, and MUST return it

3

Syntax: add_filter( $hook_name, $callback_function, $priority, $accepted_args )

4

Common filters: the_content, excerpt_length, document_title_parts

Example
// Example: Add content after every post
add_filter( 'the_content', 'add_cta_after_content' );
function add_cta_after_content( $content ) {
    if ( is_single() && is_main_query() ) {
        $cta = '<div class="post-cta">Thanks for reading! Subscribe for more.</div>';
        $content .= $cta;
    }
    return $content;  // MUST return the modified content
}

// Example: Change excerpt length
add_filter( 'excerpt_length', 'custom_excerpt_length' );
function custom_excerpt_length( $length ) {
    return 30;  // Return 30 words instead of default 55
}

// Example: Modify login page logo URL
add_filter( 'login_headerurl', 'custom_login_url' );
function custom_login_url() {
    return home_url();  // Link to homepage instead of wordpress.org
}
Filters MUST return a value. Forgetting to return breaks the feature.
4

Understand Priority and Accepted Arguments

1

Priority (default 10): Controls execution order

2

Lower priority = runs earlier. Higher = runs later

3

Use priority 5 to run before defaults, 20+ to run after

4

Accepted Args (default 1): How many parameters your function receives

5

Some hooks pass multiple values - check documentation

Example
// Example: Priority in action
add_action( 'wp_head', 'my_early_function', 5 );   // Runs early
add_action( 'wp_head', 'my_late_function', 20 );   // Runs late

// Example: Multiple arguments in filter
add_filter( 'the_title', 'modify_title', 10, 2 );
function modify_title( $title, $post_id ) {
    // $title is first argument, $post_id is second
    if ( $post_id === 42 ) {
        $title = 'Special: ' . $title;
    }
    return $title;
}
5

Know the Most Common Hooks

1

init: WordPress loaded, ready for custom post types, taxonomies

2

wp_enqueue_scripts: Properly load CSS and JavaScript on front-end

3

admin_menu: Add items to WordPress admin menu

4

wp_head: Output content in <head> section

5

wp_footer: Output content before </body>

6

the_content: Modify post/page content before display

7

excerpt_length: Change auto-generated excerpt word count

8

pre_get_posts: Modify main query before database call

Verification Checklist

  • Understand difference between Actions (do) and Filters (change)
  • Can use add_action() to run code at specific WordPress events
  • Can use add_filter() to modify data and always return it
  • Understand priority: lower numbers run first
  • Know common hooks: init, wp_enqueue_scripts, the_content

Pro Tips

  • Search the WordPress codebase for do_action() and apply_filters() to find hooks
  • Use remove_action() and remove_filter() to unhook existing functions
  • Create your own hooks with do_action() and apply_filters() in plugins
  • Anonymous functions work but named functions are easier to remove/debug
  • The WordPress Plugin API documentation lists all available hooks