Understand WordPress actions and filters with practical examples

WordPress is built on a powerful hook system that lets developers modify nearly every aspect of the platform without touching core files. At the heart of this system are two types of hooks: actions and filters. Understanding the difference between them is one of the most important skills any WordPress developer can have. Actions allow you to do something at a specific point in execution — send an email, insert a record, or load a script. Filters allow you to modify data before it is returned or displayed — change a title, adjust a price, or rewrite a URL. Both types accept a priority argument that controls the order in which callbacks run; the default is 10 and lower numbers run first. You register an action with add_action() and trigger it with do_action(). You register a filter with add_filter() and trigger it with apply_filters(). Hooks defined in themes or plugins fire when WordPress includes those files, so hook registration always happens at the top level of your file, not inside another function. Once you are comfortable with basic hooks, you will find yourself using them constantly — for example, when you want to schedule recurring tasks with WP-Cron or when you need to enqueue scripts and styles the right way. This guide covers the most common patterns with real-world examples you can drop straight into your functions.php file.

Problem: You need to run custom code at a specific point in WordPress (action) or modify data before it is output (filter), but you are not sure of the correct syntax or where to place your code.

Solution: Add the following examples to your functions.php file:

/**
 * ACTION EXAMPLE
 * Send an admin email when a new user registers.
 */
add_action( 'user_register', 'helloadmin_notify_on_register', 10, 1 );
function helloadmin_notify_on_register( $user_id ) {
    $user = get_userdata( $user_id );
    wp_mail(
        get_option( 'admin_email' ),
        'New user registered',
        'Username: ' . $user->user_login
    );
}

/**
 * FILTER EXAMPLE
 * Append a custom suffix to every post title.
 */
add_filter( 'the_title', 'helloadmin_append_title_suffix', 10, 2 );
function helloadmin_append_title_suffix( $title, $id = null ) {
    if ( is_admin() || ! in_the_loop() ) {
        return $title;
    }
    return $title . ' — helloadmin.com';
}

/**
 * CUSTOM HOOK EXAMPLE
 * Define your own action in a plugin or theme.
 */
function helloadmin_do_before_profile() {
    do_action( 'helloadmin_before_profile' );
}
// Other plugins/themes can now hook into it:
add_action( 'helloadmin_before_profile', 'my_custom_profile_banner' );
function my_custom_profile_banner() {
    echo '<div class="profile-banner">Welcome!</div>';
}

NOTE: Always check is_admin() or the current context inside filter callbacks to avoid unintended side-effects on admin screens. When removing a hook added by a third-party plugin, make sure you use the exact same priority and argument count as the original add_action() / add_filter() call, otherwise remove_action() will silently fail.