Menü schliessen
Created: July 25th 2025
Last updated: July 25th 2025
Categories: IT Development,  Wordpress
Author: Ian Walser

Understanding the WordPress Hook System: Action vs Filter Hooks

Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

Introduction

If you're new to WordPress development, you'll quickly encounter something called "hooks." They're everywhere — in themes, plugins, and even in WordPress core. Understanding how action and filter hooks work is essential for writing flexible, efficient, and WordPress-compliant code.

In this guide, you'll learn:

  • What WordPress hooks are
  • The difference between action and filter hooks
  • How to use "add_action" and "add_filter"
  • A practical example of a custom plugin using both types of hooks

What Are WordPress Hooks?

Hooks are built-in WordPress functions that allow you to "hook into" the WordPress core at specific points to modify default behavior or add custom features — without modifying core files.

There are two main types of hooks:

  • Action Hooks: Used to execute custom code at specific points.
  • Filter Hooks: Used to modify data before it is output or saved.

Action Hooks

Action hooks are triggered by specific events. You use them when you want your function to do something — like sending an email, adding a script, or modifying the admin dashboard.

Basic syntax:

add_action( 'hook_name', 'your_function_name', [priority], [accepted_args] );

Example: Hooking into "wp_footer" to output a custom message:

function custom_footer_message() {
    echo '<p>Thanks for visiting our site!</p>';
}
add_action( 'wp_footer', 'custom_footer_message' );

Filter Hooks

Filter hooks are used to change or "filter" data before it is saved or displayed. You return the modified value in your function.

Basic syntax:

add_filter( 'hook_name', 'your_function_name', [priority], [accepted_args] );

Example: Modifying the WordPress login logo URL:

function custom_login_logo_url() {
    return 'https://yourwebsite.com';
}
add_filter( 'login_headerurl', 'custom_login_logo_url' );

Creating a Simple Plugin Using Action and Filter Hooks

Let’s build a basic plugin that uses both an action and a filter. This plugin will:

  • Add a thank-you message in the footer using "add_action"
  • Modify the site title using "add_filter"

Step 1: Create Your Plugin File

Create a new folder in "wp-content/plugins" called "custom-hooks-demo". Inside that, create a file named "custom-hooks-demo.php".

Here’s the starter code:

<?php
/**
 * Plugin Name: Custom Hooks Demo
 * Description: Demonstrates action and filter hooks in WordPress.
 * Version: 1.0
 * Author: Foo Bar
 */

// Action Hook Example: Add footer message
function chd_add_footer_message() {
    echo '<p style="text-align:center;">Thanks for reading this blog!</p>';
}
add_action( 'wp_footer', 'chd_add_footer_message' );

// Filter Hook Example: Modify the site title
function chd_modify_bloginfo( $output, $show ) {
    if ( $show === 'name' ) {
        $output = 'coolest website ever: ' . $output;
    }
    return $output;
}
add_filter( 'bloginfo', 'chd_modify_bloginfo', 10, 2 );

Activate the plugin from the WordPress admin and visit your site. You’ll see:

  • A custom message in the footer
  • Your site name prefixed with "coolest website ever" in the browser title and site header (if "bloginfo('name')" is used there)

Action vs Filter: Quick Comparison

Feature Action Hook Filter Hook
Purpose Performs tasks Modifies data
Return Required? No Yes
Typical Use Enqueue scripts, send emails Alter titles, content, URLs

Bonus: How to Find Available Hooks

  • Search the WordPress Developer Reference: developer.wordpress.org/reference/
  • Use plugins like Query Monitor to inspect hooks on the page
  • Explore source code for "do_action()" and "apply_filters()" calls

Conclusion

Mastering WordPress hooks — both actions and filters — is a game-changer for any WordPress developer, especially beginners looking to build custom plugins or themes. They are at the heart of WordPress extensibility and plugin interoperability.

By using "add_action" and "add_filter", you can control how WordPress behaves at almost any point in its execution lifecycle — without ever touching core files.

Keep exploring, and happy coding!

Want More Tutorials?

Stay tuned for upcoming guides on custom post types, shortcodes, and Gutenberg block development.