Action vs Filter in WordPress: Complete Guide with Examples

Action vs Filter in WordPress: Complete Guide with Examples

What Are WordPress Hooks And Why Should You Care About WordPress Hooks

If you want WordPress to send an email every time a new post is published on WordPress or you want to add a custom message to every product description in WooCommerce or you need to resize images differently than WordPress does by default on WordPress then you have to figure out how to do it.

You do not want to change the core WordPress files because that is an idea that will cause problems with every WordPress update.

The solution to this problem is WordPress Hooks, the functions add_action and add_filter.

These two WordPress functions are the parts of WordPress plugin and theme development. They are how your code can work with the processes of WordPress in a clean and safe way that will not break when WordPress is updated. Once you understand how WordPress hooks work you will be able to do a lot of things with WordPress.

In this guide you will learn what add_action and add_filter are, how they are different, from each other when to use each WordPress function. You will see real code examples that you can use with WordPress today.

Understanding the WordPress Hook System

So you want to know about the WordPress hook system. Well lets start with the basics. WordPress is like a factory that makes web pages. When you ask for a page it does a lot of things. It gets information from the database finds the posts you want and makes the HTML code.

As it does all these things it stops at different points and says “Hey is anyone there? Do you want to add something ” These points are called hooks. You can write your code and attach it to these hooks so it runs at the right time.

Think of it like an assembly line. WordPress goes through steps to make a web page. At each step it checks if you want to do something. If you do your code runs.

There are two types of hooks in WordPress:

  • Action Hooks — Let you do something at a specific point (like sending an email or adding HTML)
  • Filter Hooks — Let you modify data before it’s used (like changing the content of a post)

WordPress hook execution lifecycle diagram showing where action hooks and filter hooks fire during page load

What Is add_action() in WordPress?

The add_action() function helps you connect your function to a specific action, in WordPress.

When WordPress gets to that action it will run your function.

Action hooks are used for taking actions. They do not give back any results.

You are telling WordPress. When you get to this point also please run my function with add_action().

You use add_action() to do things.

The Syntax of add_action()

add_action( $hook_name, $callback, $priority, $accepted_args );

Here’s what each parameter means:

  • $hook_name — The name of the action hook you want to attach to (e.g., ‘init’, ‘wp_head’, ‘save_post’)
  • $callback — The name of your function that should run when the hook fires
  • $priority — (Optional) A number that controls the order. Lower numbers run first. Default is 10
  • $accepted_args — (Optional) How many arguments your function accepts. Default is 1

A Simple add_action() Example

Let’s say you want to add a welcome message right after the closing </head> tag:

// Step 1: Register your function with the hook
add_action( 'wp_head', 'my_custom_head_code' );

// Step 2: Write the function that will run
function my_custom_head_code() {
    echo '<!-- Custom code added by my theme -->';
}

That’s it. When WordPress processes wp_head, it will automatically call my_custom_head_code() and whatever you echo inside it will appear in the HTML output.

Real-World add_action() Examples

Here are some practical examples you might actually use:

// Example 1: Enqueue a custom CSS stylesheet
add_action( 'wp_enqueue_scripts', 'my_theme_styles' );

function my_theme_styles() {
    wp_enqueue_style( 
        'my-custom-style', 
        get_template_directory_uri() . '/css/custom.css'
    );
}

// Example 2: Send an email when a new user registers
add_action( 'user_register', 'notify_admin_new_user' );

function notify_admin_new_user( $user_id ) {
    $user = get_userdata( $user_id );
    wp_mail(
        get_option( 'admin_email' ),
        'New User Registered',
        'User ' . $user->user_login . ' just signed up!'
    );
}

// Example 3: Add custom content to the footer
add_action( 'wp_footer', 'add_footer_message' );

function add_footer_message() {
    echo '<p class="my-footer-note">Thanks for visiting!</p>';
}

Pro Tip:

Always prefix your function names (e.g., mytheme_ or myplugin_) to avoid conflicts with other plugins or WordPress core functions. Function name clashes cause fatal errors.

What Is add_filter() in WordPress?

The add_filter() function works similarly to add_action(), but with one crucial difference: your function must return a value.

Filter hooks are about modifying data. WordPress passes some data (like post content, a title, a URL) through the filter, your function can change it, and then it must return the modified data for WordPress to continue using.

Think of it like a relay race: WordPress passes the baton (data) to your function, you do what you want with it, and then you pass it back.

The Syntax of add_filter()

add_filter( $hook_name, $callback, $priority, $accepted_args );

The parameters are identical to add_action(). The critical difference is in how you write your callback function — it must accept the data and return it (modified or unmodified).

A Simple add_filter() Example

Let’s add a custom note at the end of every post’s content:

add_filter( 'the_content', 'add_note_after_content' );

function add_note_after_content( $content ) {
    // $content holds the original post content
    
    $note = '<p><em>Thanks for reading! Share this post if you found it useful.</em></p>';
    
    // Return the original content + our added note
    return $content . $note;
}

Notice the key thing: the function receives $content and returns a modified version. Without the return statement, the content would disappear entirely — a common beginner mistake.

Real-World add_filter() Examples

// Example 1: Change the default "Read More" text
add_filter( 'the_content_more_link', 'custom_read_more_text' );

function custom_read_more_text( $link ) {
    return str_replace( 'Read More', 'Continue Reading →', $link );
}

// Example 2: Modify the login error message (for security)
add_filter( 'login_errors', 'vague_login_error' );

function vague_login_error( $error ) {
    return 'Login failed. Please try again.';
}

// Example 3: Change the excerpt length
add_filter( 'excerpt_length', 'custom_excerpt_length' );

function custom_excerpt_length( $length ) {
    return 30; // 30 words instead of WordPress default 55
}

// Example 4: Add a CSS class to body tag
add_filter( 'body_class', 'add_custom_body_class' );

function add_custom_body_class( $classes ) {
    $classes[] = 'my-custom-class';
    return $classes;
}

Common Mistake:

Forgetting to return the value in a filter callback is the #1 beginner mistake. If you use add_filter() but don’t return anything from your function, WordPress gets null instead of the data — which can blank out content or break your site.

add_action vs add_filter: Key Differences

Now that you’ve seen both, let’s put them side by side. Understanding when to use which one is the most important skill here.

Feature add_action() add_filter()
Purpose Execute code at a point in time Modify data before it’s used
Return value Not required (void) Required — must return data
Receives data? Sometimes (optional) Always (at minimum 1 arg)
Side effects Common (echo, wp_mail, etc.) Data transformation only
Typical uses Enqueue scripts, send emails, log data Modify content, titles, queries
If you forget return No problem Data becomes null — site breaks
Can it echo HTML? Yes, common Return HTML instead

Comparison diagram between WordPress add_action and add_filter showing difference in data flow and return values"

The Simple Mental Model

Here’s a quick way to decide which to use:

  • Ask yourself: “Am I doing something (running code, sending email, adding HTML)?” → Use add_action()
  • Ask yourself: “Am I changing something that already exists (content, settings, data)?” → Use add_filter()

Understanding Priority in WordPress Hooks

Both add_action() and add_filter() accept a $priority parameter. This controls the order in which multiple functions attached to the same hook run.

The default priority is 10. Functions with a lower number run first.

  • Priority 1–4: runs before almost everything
  • Priority 10: standard, runs in normal order
  • Priority 20+: runs after most other hooks
  • Priority 999: runs after virtually everything
// This runs FIRST (priority 5)
add_action( 'wp_footer', 'my_early_footer', 5 );
function my_early_footer() {
    echo 'I run first';
}

// This runs SECOND (priority 10, the default)
add_action( 'wp_footer', 'my_normal_footer' );
function my_normal_footer() {
    echo 'I run second';
}

// This runs LAST (priority 20)
add_action( 'wp_footer', 'my_late_footer', 20 );
function my_late_footer() {
    echo 'I run last';
}

Passing Arguments to Your Hook Functions

Sometimes, WordPress passes multiple pieces of data along with a hook. The 4th parameter in both add_action() and add_filter()$accepted_args — tells WordPress how many of those arguments your function wants to receive.

// save_post passes 3 args: $post_id, $post, $update
// We tell WordPress we want all 3 with the last parameter
add_action( 'save_post', 'handle_post_save', 10, 3 );

function handle_post_save( $post_id, $post, $update ) {
    if ( $update ) {
        // This is an existing post being updated
        error_log( 'Post updated: ' . $post_id );
    } else {
        // This is a brand new post
        error_log( 'New post created: ' . $post_id );
    }
}
// comment_text filter passes comment content + the comment object
add_filter( 'comment_text', 'modify_comment_text', 10, 2 );

function modify_comment_text( $text, $comment ) {
    // Check if this comment is by an admin
    if ( user_can( $comment->user_id, 'manage_options' ) ) {
        $text = '<span class="admin-badge">Admin</span> ' . $text;
    }
    return $text;
}

Creating Your Own Custom Hooks

Here’s something a lot of beginners don’t realize: you’re not limited to WordPress’s built-in hooks. You can create your own hooks inside your plugin or theme, making your code extensible for others (or yourself).

Creating a Custom Action Hook

Use do_action() to fire a custom action hook:

// In your plugin, fire a custom action
function my_plugin_process_order( $order_id ) {
    // ... do your order processing ...
    
    // Fire custom hook — lets others extend this
    do_action( 'my_plugin_after_order_processed', $order_id );
}

// Now anyone (other plugins, themes) can hook in:
add_action( 'my_plugin_after_order_processed', 'send_order_confirmation' );

function send_order_confirmation( $order_id ) {
    // Send an email, log to database, etc.
}

Creating a Custom Filter Hook

Use apply_filters() to create a filterable value:

function my_plugin_get_button_text() {
    // Default text, but filterable by others
    $text = 'Buy Now';
    
    return apply_filters( 'my_plugin_button_text', $text );
}

// Someone else (or your theme) can change this:
add_filter( 'my_plugin_button_text', 'change_button_to_spanish' );

function change_button_to_spanish( $text ) {
    return 'Comprar Ahora';
}

Removing Hooks with remove_action() and remove_filter()

Just as you can add hooks, you can remove them. This is useful when you want to disable something a plugin or theme has added.

// Remove an action hook
remove_action( 'wp_head', 'wp_generator' ); // Hides WP version number

// Remove a filter hook
remove_filter( 'the_content', 'wpautop' ); // Removes auto <p> tags

// IMPORTANT: Use the same priority that was used when adding!
// If a hook was added at priority 20, remove it at priority 20
remove_action( 'some_hook', 'some_function', 20 );

Important Note on Removing Hooks

To successfully remove a hook, you must use the exact same priority that was used when adding it. If you’re not sure what priority a plugin used, check its source code. The default is 10.

Most Commonly Used WordPress Hooks

WordPress has hundreds of hooks. Here are the ones you’ll use most often as a developer:

Essential Action Hooks

Hook Name When It Fires Common Use
init After WordPress loads Register post types, taxonomies
wp_enqueue_scripts Before scripts/styles load Add custom CSS/JS files
wp_head Inside <head> tag Add meta tags, inline scripts
wp_footer Before </body> Add tracking scripts, HTML
save_post When post is saved Custom meta saving, notifications
admin_menu Admin menu builds Add custom admin pages
user_register New user registration Welcome emails, default roles

Essential Filter Hooks

Hook Name When It Fires Common Use
the_content Post/page content Add banners, ads, custom HTML
the_title Post/page title Prefix titles, add icons
excerpt_length Excerpt word count Change excerpt length
body_class Body element classes Add conditional CSS classes
login_errors Login error messages Vague error messages (security)
wp_nav_menu_items Navigation menu HTML Add items, login/logout links
upload_mimes Allowed upload types Allow SVG or custom file types

Best Practices for Using Hooks in WordPress

Using hooks correctly is as important as knowing what they do. Here are essential best practices every developer should follow:

1. Always Prefix Function and Hook Names

Use a unique prefix based on your theme or plugin to avoid naming conflicts:

// Bad — too generic, might conflict
add_action( 'init', 'setup_things' );

// Good — unique prefix prevents conflicts
add_action( 'init', 'myplugin_setup_things' );

2. Use Conditional Logic in Hooks

add_action( 'wp_enqueue_scripts', 'load_scripts_only_on_contact' );

function load_scripts_only_on_contact() {
    // Only load heavy scripts on the contact page
    if ( is_page( 'contact' ) ) {
        wp_enqueue_script( 'my-contact-script', plugin_dir_url( __FILE__ ) . 'js/contact.js' );
    }
}

3. Check Nonces for Security on Form Submissions

add_action( 'save_post', 'securely_save_meta' );

function securely_save_meta( $post_id ) {
    // Verify nonce before saving
    if ( ! isset( $_POST['my_nonce'] ) || ! wp_verify_nonce( $_POST['my_nonce'], 'save_my_meta' ) ) {
        return;
    }
    // Safe to proceed...
}

4. Don’t Echo Inside Filters

Filters should return data, not echo it. Echoing inside a filter function causes the output to appear in the wrong place (usually at the very top of the page before any HTML).

5. Keep Hook Functions Focused

Each function hooked should do one specific thing. Don’t jam 10 different tasks into a single hooked function — it makes debugging difficult and reduces reusability.

 

We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners. View more
Cookies settings
Accept
Privacy & Cookie policy
Privacy & Cookies policy
Cookie name Active
Last updated: May 14, 2022 Please read these terms and conditions carefully before using Our Service.

Interpretation and Definitions

Interpretation

The words of which the initial letter is capitalized have meanings defined under the following conditions. The following definitions shall have the same meaning regardless of whether they appear in singular or in plural.

Definitions

For the purposes of these Terms and Conditions:
  • Affiliate means an entity that controls, is controlled by or is under common control with a party, where "control" means ownership of 50% or more of the shares, equity interest or other securities entitled to vote for election of directors or other managing authority.
  • Country refers to: Gujarat, India
  • Company (referred to as either "the Company", "We", "Us" or "Our" in this Agreement) refers to Magexweb Infotech, D-1002, Jasmin Green 1, Near Vaishnov Devi Circle, Ahmedabad -382421.
  • Device means any device that can access the Service such as a computer, a cellphone or a digital tablet.
  • Service refers to the Website.
  • Terms and Conditions (also referred as "Terms") mean these Terms and Conditions that form the entire agreement between You and the Company regarding the use of the Service. This Terms and Conditions agreement has been created with the help of the Terms and Conditions Generator.
  • Third-party Social Media Service means any services or content (including data, information, products or services) provided by a third-party that may be displayed, included or made available by the Service.
  • Website refers to Veducator, accessible from https://www.veducator.com
  • You means the individual accessing or using the Service, or the company, or other legal entity on behalf of which such individual is accessing or using the Service, as applicable.

Acknowledgment

These are the Terms and Conditions governing the use of this Service and the agreement that operates between You and the Company. These Terms and Conditions set out the rights and obligations of all users regarding the use of the Service. Your access to and use of the Service is conditioned on Your acceptance of and compliance with these Terms and Conditions. These Terms and Conditions apply to all visitors, users and others who access or use the Service. By accessing or using the Service You agree to be bound by these Terms and Conditions. If You disagree with any part of these Terms and Conditions then You may not access the Service. You represent that you are over the age of 18. The Company does not permit those under 18 to use the Service. Your access to and use of the Service is also conditioned on Your acceptance of and compliance with the Privacy Policy of the Company. Our Privacy Policy describes Our policies and procedures on the collection, use and disclosure of Your personal information when You use the Application or the Website and tells You about Your privacy rights and how the law protects You. Please read Our Privacy Policy carefully before using Our Service.

Links to Other Websites

Our Service may contain links to third-party web sites or services that are not owned or controlled by the Company. The Company has no control over, and assumes no responsibility for, the content, privacy policies, or practices of any third party web sites or services. You further acknowledge and agree that the Company shall not be responsible or liable, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with the use of or reliance on any such content, goods or services available on or through any such web sites or services. We strongly advise You to read the terms and conditions and privacy policies of any third-party web sites or services that You visit.

Termination

We may terminate or suspend Your access immediately, without prior notice or liability, for any reason whatsoever, including without limitation if You breach these Terms and Conditions. Upon termination, Your right to use the Service will cease immediately.

Limitation of Liability

Notwithstanding any damages that You might incur, the entire liability of the Company and any of its suppliers under any provision of this Terms and Your exclusive remedy for all of the foregoing shall be limited to the amount actually paid by You through the Service or 100 USD if You haven't purchased anything through the Service. To the maximum extent permitted by applicable law, in no event shall the Company or its suppliers be liable for any special, incidental, indirect, or consequential damages whatsoever (including, but not limited to, damages for loss of profits, loss of data or other information, for business interruption, for personal injury, loss of privacy arising out of or in any way related to the use of or inability to use the Service, third-party software and/or third-party hardware used with the Service, or otherwise in connection with any provision of this Terms), even if the Company or any supplier has been advised of the possibility of such damages and even if the remedy fails of its essential purpose. Some states do not allow the exclusion of implied warranties or limitation of liability for incidental or consequential damages, which means that some of the above limitations may not apply. In these states, each party's liability will be limited to the greatest extent permitted by law.

"AS IS" and "AS AVAILABLE" Disclaimer

The Service is provided to You "AS IS" and "AS AVAILABLE" and with all faults and defects without warranty of any kind. To the maximum extent permitted under applicable law, the Company, on its own behalf and on behalf of its Affiliates and its and their respective licensors and service providers, expressly disclaims all warranties, whether express, implied, statutory or otherwise, with respect to the Service, including all implied warranties of merchantability, fitness for a particular purpose, title and non-infringement, and warranties that may arise out of course of dealing, course of performance, usage or trade practice. Without limitation to the foregoing, the Company provides no warranty or undertaking, and makes no representation of any kind that the Service will meet Your requirements, achieve any intended results, be compatible or work with any other software, applications, systems or services, operate without interruption, meet any performance or reliability standards or be error free or that any errors or defects can or will be corrected. Without limiting the foregoing, neither the Company nor any of the company's provider makes any representation or warranty of any kind, express or implied: (i) as to the operation or availability of the Service, or the information, content, and materials or products included thereon; (ii) that the Service will be uninterrupted or error-free; (iii) as to the accuracy, reliability, or currency of any information or content provided through the Service; or (iv) that the Service, its servers, the content, or e-mails sent from or on behalf of the Company are free of viruses, scripts, trojan horses, worms, malware, timebombs or other harmful components. Some jurisdictions do not allow the exclusion of certain types of warranties or limitations on applicable statutory rights of a consumer, so some or all of the above exclusions and limitations may not apply to You. But in such a case the exclusions and limitations set forth in this section shall be applied to the greatest extent enforceable under applicable law.

Governing Law

The laws of the Country, excluding its conflicts of law rules, shall govern this Terms and Your use of the Service. Your use of the Application may also be subject to other local, state, national, or international laws.

Disputes Resolution

If You have any concern or dispute about the Service, You agree to first try to resolve the dispute informally by contacting the Company.

For European Union (EU) Users

If You are a European Union consumer, you will benefit from any mandatory provisions of the law of the country in which you are resident in.

United States Legal Compliance

You represent and warrant that (i) You are not located in a country that is subject to the United States government embargo, or that has been designated by the United States government as a "terrorist supporting" country, and (ii) You are not listed on any United States government list of prohibited or restricted parties.

Severability and Waiver

Severability

If any provision of these Terms is held to be unenforceable or invalid, such provision will be changed and interpreted to accomplish the objectives of such provision to the greatest extent possible under applicable law and the remaining provisions will continue in full force and effect.

Waiver

Except as provided herein, the failure to exercise a right or to require performance of an obligation under these Terms shall not effect a party's ability to exercise such right or require such performance at any time thereafter nor shall the waiver of a breach constitute a waiver of any subsequent breach.

Translation Interpretation

These Terms and Conditions may have been translated if We have made them available to You on our Service. You agree that the original English text shall prevail in the case of a dispute.

Changes to These Terms and Conditions

We reserve the right, at Our sole discretion, to modify or replace these Terms at any time. If a revision is material We will make reasonable efforts to provide at least 30 days' notice prior to any new terms taking effect. What constitutes a material change will be determined at Our sole discretion. By continuing to access or use Our Service after those revisions become effective, You agree to be bound by the revised terms. If You do not agree to the new terms, in whole or in part, please stop using the website and the Service.

Contact Us

If you have any questions about these Terms and Conditions, You can contact us:
Save settings
Cookies settings