Paramclasses Academy - Blog

Tutorials Details

←
Post 34 of 40
→

What is WordPress Hooks: Actions and Filters for Developers ?

What is WordPress Hooks: Actions and Filters for Developers ?

November 17, 2026

1. Introduction

WordPress is a powerful content management system built with PHP. One of the main reasons WordPress is so flexible is its Hook System.

As a WordPress developer, you may want to add custom functionality, modify post content, change titles, add scripts, or customize how plugins and themes behave.

You can accomplish many of these tasks using WordPress Hooks without modifying WordPress core files.

For example:

  • Add a custom message to the website footer.

  • Modify post titles before displaying them.

  • Add custom CSS classes to the body element.

  • Execute code when a post is published.

  • Change the output of a WordPress function.

  • Extend plugin and theme functionality.

WordPress provides two major types of hooks:

  1. Action Hooks

  2. Filter Hooks

In this tutorial, you will learn both types with practical PHP examples.

By the end, you will understand how to create, register, execute, and customize WordPress hooks like a developer.

2. What Are WordPress Hooks?

A WordPress Hook is a predefined point in the execution of WordPress where developers can connect their own PHP functions.

Hooks allow your custom code to interact with WordPress Core, themes, and plugins.

Think of a hook as a connection point.

When WordPress reaches that point, it can execute your registered function or allow your function to modify some data.

For example:

function param_custom_message() {

    echo "Welcome to Param Classes!";

}

add_action(
    'wp_footer',
    'param_custom_message'
);

In this example:

  • param_custom_message() is a custom PHP function.

  • wp_footer is an existing WordPress action hook.

  • add_action() registers our function with that hook.

When WordPress executes the wp_footer hook, our custom function runs.

Official documentation: WordPress Plugin Handbook – Hooks.

3. Types of WordPress Hooks

WordPress provides two primary types of hooks.

A. Action Hooks

Action Hooks allow developers to execute custom code at a specific point in the WordPress execution process.

They are useful when you want WordPress to perform an operation.

Examples:

  • Display a custom message.

  • Register a custom post type.

  • Load CSS and JavaScript files.

  • Execute code when a post is published.

  • Add content to a website footer.

The main function used to register an action is:

add_action();

B. Filter Hooks

Filter Hooks allow developers to modify existing data before WordPress uses or displays it.

They are useful when you want to change a value.

Examples:

  • Modify a post title.

  • Change post content.

  • Customize an excerpt.

  • Add a custom CSS class.

  • Modify text generated by a theme or plugin.

The main function used to register a filter is:

add_filter();

4. Difference Between Action Hooks and Filter Hooks

Understand this difference carefully.

Action Hook: Executes a function to perform a task.

Filter Hook: Receives a value, modifies it, and returns the updated value.

Example:

// Action Hook

function param_welcome_message() {

    echo "Welcome to Param Classes!";

}

add_action(
    'wp_footer',
    'param_welcome_message'
);

Here, WordPress executes a function that displays a message.

Now consider a filter:

// Filter Hook

function param_modify_title( $title ) {

    return 'Tutorial: ' . $title;

}

add_filter(
    'the_title',
    'param_modify_title'
);

Here, WordPress passes a title to the function, and the function returns a modified title.

Remember these important rules:

  • Action callbacks normally perform operations and do not return a value to the action hook.

  • Filter callbacks must return the filtered value.

  • Filters should modify the supplied data without performing unrelated side effects.

  • Both actions and filters can have multiple callback functions.

5. Understanding Callback Functions

A callback function is a function that you register with a hook so WordPress can execute it at the appropriate time.

Example:

function param_custom_callback() {

    echo "My first WordPress callback!";

}

add_action(
    'wp_footer',
    'param_custom_callback'
);

Let's understand the code.

Step 1: Create a function

function param_custom_callback() {

    echo "My first WordPress callback!";

}

This creates a PHP function.

Step 2: Register the callback

add_action(
    'wp_footer',
    'param_custom_callback'
);

This tells WordPress to execute the function when the wp_footer action runs.

The callback does not need to be executed manually.

WordPress calls it through the hook.

6. Understanding add_action()

The add_action() function registers a callback function with an existing action hook.

Basic syntax:

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

Let's understand its parameters.

1. $hook_name

The name of the action hook.

Example:

'wp_footer'

2. $callback

The name of the function that WordPress should execute.

Example:

'param_custom_message'

3. $priority

Determines the execution order of callbacks registered on the same hook.

The default priority is 10.

A smaller number runs earlier than a larger number.

4. $accepted_args

Specifies how many arguments the callback function accepts from the hook.

The default is 1.

Official documentation: add_action() Function Reference.

7. Practical Example 1: Add a Custom Message to the Website Footer

Now let's create our first practical project.

We will add a message to the footer of a WordPress website using an action hook.

Step 1: Open Your WordPress Project

Open your WordPress installation.

If you are using the child theme from Tutorial 33, open:

wp-content/
    themes/
        astra-child/
            functions.php

You can also use a suitable custom plugin or a trusted code snippets plugin.

Step 2: Open functions.php

Open the child theme's functions.php file.

Add this code:

<?php

function param_footer_message() {

    echo '<p class="param-footer-message">
        Welcome to Param Classes!
    </p>';

}

add_action(
    'wp_footer',
    'param_footer_message'
);

If your functions.php already starts with <?php, do not add another PHP opening tag.

Step 3: Save the File

Save the updated functions.php file.

Step 4: Open Your Website

Visit your WordPress website frontend.

Scroll down to the bottom of the page.

You should see:

Welcome to Param Classes!

Step 5: Add Custom CSS

To improve the appearance, open your child theme's style.css file.

Add:

.param-footer-message {

    text-align: center;

    color: #ea580c;

    font-size: 16px;

    font-weight: 600;

    padding: 15px;

}

Save the file and refresh your website.

Important: This example requires the active theme to call wp_footer() in its footer template. Without that template hook, the callback will not run at the expected location.

8. Understanding Hook Priority

Priority determines the order in which registered callbacks execute on the same hook.

The default priority is 10.

Let's understand with an example.

function param_first_message() {

    echo "First Message<br>";

}

function param_second_message() {

    echo "Second Message<br>";

}

function param_third_message() {

    echo "Third Message<br>";

}

add_action(
    'wp_footer',
    'param_first_message',
    5
);

add_action(
    'wp_footer',
    'param_second_message',
    10
);

add_action(
    'wp_footer',
    'param_third_message',
    20
);

Expected execution order:

First Message
Second Message
Third Message

Why?

  • Priority 5 executes first.

  • Priority 10 executes second.

  • Priority 20 executes third.

Remember:

  • Lower priority number means earlier execution.

  • Higher priority number means later execution.

  • When priorities are equal, callbacks normally run in the order they were registered.

9. Understanding accepted_args

Some WordPress hooks pass additional information to callback functions.

For example, the save_post action can provide:

  • Post ID.

  • Post object.

  • Whether the post is being updated.

Example:

function param_post_saved(
    $post_id,
    $post,
    $update
) {

    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    if ( wp_is_post_autosave( $post_id ) ) {
        return;
    }

    if ( 'post' !== $post->post_type ) {
        return;
    }

    if ( $update ) {

        error_log(
            'A post was updated: ' . $post_id
        );

    } else {

        error_log(
            'A new post was created: ' . $post_id
        );

    }

}

add_action(
    'save_post',
    'param_post_saved',
    10,
    3
);

Let's understand the final line:

add_action(
    'save_post',
    'param_post_saved',
    10,
    3
);
  • save_post is the action hook.

  • param_post_saved is the callback function.

  • 10 is the priority.

  • 3 means the callback accepts three arguments.

The callback receives the values passed by the corresponding save_post action.

The revision and autosave checks help avoid treating those operations as ordinary post updates.

The example uses error_log() for learning purposes. In a production application, use an appropriate logging system rather than exposing internal messages to visitors.

Official reference: save_post Action Hook.

10. Understanding add_filter()

The add_filter() function registers a callback function with an existing filter hook.

Basic syntax:

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

Its parameters work similarly to add_action().

The important difference is that a filter callback receives data and must return the modified value.

Official documentation: add_filter() Function Reference.

11. Practical Example 2: Modify WordPress Post Titles

Now let's use a filter hook.

We will add the word Tutorial: before post titles.

Step 1: Open functions.php

Open:

astra-child/functions.php

Step 2: Add the Filter Code

function param_modify_post_title( $title ) {

    if ( is_admin() ) {
        return $title;
    }

    if ( ! in_the_loop() ) {
        return $title;
    }

    if ( ! is_main_query() ) {
        return $title;
    }

    return 'Tutorial: ' . $title;

}

add_filter(
    'the_title',
    'param_modify_post_title'
);

Step 3: Save the File

Save your functions.php file.

Step 4: Open the Website

Open a blog post or a post listing on your website.

You should see a title similar to:

Tutorial: WordPress Development

The original title is passed into the filter, and our callback returns a modified version.

Important: The the_title filter can run in multiple contexts. The checks in this example limit the modification to the main frontend query loop. If you want to change titles in a different context, adjust the conditions accordingly.

12. Practical Example 3: Modify WordPress Post Content

The the_content filter allows developers to modify post content before it is displayed.

Let's add a custom message after the post content.

Add this code to your child theme's functions.php file:

function param_add_content_message( $content ) {

    if ( is_admin() ) {
        return $content;
    }

    if ( ! is_singular( 'post' ) ) {
        return $content;
    }

    if ( ! in_the_loop() || ! is_main_query() ) {
        return $content;
    }

    $message = '<div class="param-content-message">
        Thanks for reading this tutorial!
    </div>';

    return $content . $message;

}

add_filter(
    'the_content',
    'param_add_content_message'
);

Understanding the Code

$content

Contains the original post content.

is_singular( 'post' )

Checks whether the current page is displaying a single post.

in_the_loop()

Checks whether WordPress is currently inside the main post loop.

is_main_query()

Checks whether the current query is the main WordPress query.

return $content . $message

Combines the original content with the additional message and returns the modified content.

Add CSS Styling

Open your child theme's style.css file.

Add:

.param-content-message {

    background-color: #fff7ed;

    border-left: 4px solid #ea580c;

    padding: 15px;

    margin-top: 25px;

    color: #7c2d12;

    border-radius: 5px;

}

Save the file and refresh a single blog post.

You should see the custom message after the post content.

13. Practical Example 4: Add a Custom Body Class

WordPress provides the body_class filter to modify the CSS classes applied to the body element.

Example:

function param_custom_body_class( $classes ) {

    $classes[] = 'param-website';

    return $classes;

}

add_filter(
    'body_class',
    'param_custom_body_class'
);

This adds:

<body class="param-website">

The actual body element may contain additional WordPress and theme classes.

Now you can target the custom class using CSS:

.param-website {

    background-color: #f8fafc;

}

This is useful when you want to apply website-wide CSS using a class added through WordPress.

14. Practical Example 5: Load Custom CSS Using an Action Hook

WordPress provides the wp_enqueue_scripts action hook for loading frontend stylesheets and JavaScript files.

Example:

function param_custom_styles() {

    wp_enqueue_style(
        'param-custom-style',
        get_stylesheet_directory_uri() . '/custom.css',
        array(),
        '1.0.0'
    );

}

add_action(
    'wp_enqueue_scripts',
    'param_custom_styles'
);

Step 1: Create custom.css

Inside your child theme directory, create:

astra-child/
│
├── style.css
├── functions.php
└── custom.css

Step 2: Add CSS

Inside custom.css:

body {

    font-family: Arial, sans-serif;

}

h1 {

    letter-spacing: 1px;

}

Step 3: Save and Refresh

Save both files.

Open your website frontend and inspect the page source or browser developer tools to confirm that the stylesheet loads.

Important: This example loads an additional CSS file. If the styles can be maintained in the child theme's existing style.css, a separate file may not be necessary.

Official reference: wp_enqueue_scripts.

15. Create Your Own Custom Action Hook

Developers can also create custom hooks.

This is particularly useful when developing a WordPress plugin or theme that other developers may need to extend.

Let's create a custom action.

Step 1: Create a Custom Hook

Add this code to a suitable PHP file:

function param_custom_hook_area() {

    do_action( 'param_before_welcome_message' );

    echo '<h2>Welcome to Param Classes</h2>';

    do_action( 'param_after_welcome_message' );

}

Here, we have created two custom action hook locations.

Step 2: Register a Callback

Add:

function param_before_message() {

    echo '<p>Welcome to our website!</p>';

}

add_action(
    'param_before_welcome_message',
    'param_before_message'
);

Step 3: Display the Custom Hook Area

Call the function from an appropriate template or shortcode:

param_custom_hook_area();

Expected output:

Welcome to our website!

Welcome to Param Classes

The custom action executes before the heading is displayed.

The second custom action can be used to add content after the heading.

Important: A custom action hook is executed only when its corresponding do_action() call runs. Creating the hook does not automatically display anything.

16. Create Your Own Custom Filter Hook

Now let's create a custom filter.

A custom filter allows another developer to modify data generated by your theme or plugin.

Step 1: Create the Filter

Add this code:

function param_get_welcome_text() {

    $message = 'Welcome to Param Classes';

    return apply_filters(
        'param_welcome_message',
        $message
    );

}

Here:

  • apply_filters() executes the registered filter callbacks.

  • param_welcome_message is our custom filter name.

  • $message is the original value.

Step 2: Register a Filter Callback

Add:

function param_change_welcome_text( $message ) {

    return $message . ' - Learn WordPress Development';

}

add_filter(
    'param_welcome_message',
    'param_change_welcome_text'
);

Step 3: Display the Filtered Value

Call:

echo esc_html(
    param_get_welcome_text()
);

Expected output:

Welcome to Param Classes - Learn WordPress Development

This demonstrates how a custom filter allows another function to modify the original value.

Official reference: WordPress Filters Handbook.

17. Important WordPress Hooks Every Developer Should Know

WordPress provides many built-in hooks.

Here are some commonly used hooks.

Common Action Hooks

1. init

Runs after WordPress has loaded but before headers are sent. Commonly used for registering post types, taxonomies, and other initialization tasks.

2. wp_enqueue_scripts

Used to enqueue frontend CSS and JavaScript.

3. admin_enqueue_scripts

Used to enqueue scripts and styles in the WordPress administration area.

4. wp_footer

Runs near the end of the frontend page when the theme calls the corresponding template function.

5. wp_head

Runs inside the HTML head section when the theme calls wp_head().

6. save_post

Runs when a post is saved or updated.

7. transition_post_status

Runs when a post changes status.

8. widgets_init

Used to register widget areas and widgets.

Common Filter Hooks

1. the_content

Modifies post content.

2. the_title

Modifies post titles.

3. the_excerpt

Modifies post excerpts.

4. body_class

Modifies CSS classes on the body element.

5. document_title_parts

Modifies the individual parts of the document title.

6. upload_mimes

Modifies allowed upload MIME types. Use appropriate validation and security checks before allowing additional file types.

7. widget_text

Modifies text widget output in supported widget contexts.

For a broader list, visit the official WordPress Action Reference and Filter Reference.

18. Remove an Existing Hook Using remove_action() and remove_filter()

Sometimes you may want to remove a callback that another theme or plugin has registered.

WordPress provides two functions:

  • remove_action()

  • remove_filter()

A. remove_action()

Example:

remove_action(
    'wp_footer',
    'param_footer_message',
    10
);

This attempts to remove the callback registered with the specified action and priority.

B. remove_filter()

Example:

remove_filter(
    'the_title',
    'param_modify_post_title',
    10
);

This attempts to remove the registered filter callback.

Important: To remove a callback successfully, the hook name, callback, and priority must match the original registration.

You must also call the removal code after the original callback has been registered but before the hook executes.

For example, when removing a callback registered by a plugin, the correct timing may require using a suitable later action.

Anonymous functions and object methods require additional care because you need the same callable reference that was originally registered.

Official references: remove_action() and remove_filter().

19. Common WordPress Hooks Errors and Their Solutions

Problem 1: Custom Function Is Not Executing

Possible reasons:

  • Incorrect hook name.

  • Callback function not registered.

  • The hook never executes in the current request.

  • Code is placed in the wrong file.

Solution:

  • Check the spelling of the hook.

  • Confirm that the callback is registered.

  • Check whether the theme calls the required template hook.

  • Review PHP error logs.

Problem 2: Filter Changes Are Not Visible

Possible reasons:

  • The filter is not used in the current output.

  • The callback returns the original value.

  • Another callback modifies the value afterward.

  • The code is restricted by conditional checks.

Solution:

  • Confirm that the filter is applied.

  • Return the modified value.

  • Review callback priority.

  • Test the code in the correct frontend context.

Problem 3: Website Shows a Critical Error

Possible reasons:

  • PHP syntax error.

  • Undefined function.

  • Duplicate function name.

  • Incorrect callback parameters.

Solution:

  • Check the PHP error log.

  • Correct the syntax.

  • Use a unique function prefix.

  • Test code on localhost or staging before production.

Problem 4: Callback Receives Incorrect Arguments

Possible reasons:

  • Incorrect $accepted_args value.

  • Callback parameter count does not match the hook arguments.

  • Incorrect assumption about the hook's parameters.

Solution:

  • Check the official hook documentation.

  • Inspect the corresponding do_action() or apply_filters() call.

  • Set the correct $accepted_args value.

Problem 5: Custom CSS Is Not Loading

Possible reasons:

  • Incorrect stylesheet path.

  • Incorrect enqueue method.

  • Browser or server cache.

  • Parent theme CSS overrides the custom styles.

Solution:

  • Check the browser developer tools.

  • Verify the stylesheet URL.

  • Clear relevant caches.

  • Review stylesheet dependencies and CSS specificity.

20. Best Practices for WordPress Hook Development

Follow these professional development practices:

  1. Use action hooks for operations and filter hooks for data modification.

  2. Always return the modified value from filter callbacks.

  3. Use unique prefixes for custom function names.

  4. Check hook parameters in official documentation.

  5. Set callback priorities only when necessary.

  6. Use the correct $accepted_args value.

  7. Avoid editing WordPress core files.

  8. Avoid unnecessary output inside filter callbacks.

  9. Escape output using appropriate WordPress functions.

  10. Sanitize and validate incoming data.

  11. Test custom hooks on localhost or staging.

  12. Use a custom plugin for reusable functionality.

  13. Remove callbacks carefully and at the correct execution time.

  14. Document custom hooks when building plugins for other developers.

For additional guidance, read the WordPress Coding Standards.

21. Practical Assignment

Now create a small WordPress customization project using both action and filter hooks.

Task 1: Create a Custom Action

Create a function that displays:

Welcome to Param Classes!

Register it using the wp_footer action hook.

Task 2: Add Custom Styling

Use CSS to:

  • Center the message.

  • Change its text color.

  • Add padding.

  • Apply a background color.

Task 3: Modify Post Titles

Use the the_title filter to add:

Tutorial:

before the post title in the main frontend loop.

Task 4: Modify Post Content

Use the the_content filter to add a message after single blog post content.

Task 5: Create a Custom Filter

Create a filter named:

param_custom_heading

Pass an original heading through apply_filters() and modify it using add_filter().

Task 6: Test Your Website

Verify that:

  • The footer message appears.

  • Custom CSS works.

  • Post titles are modified.

  • Post content displays the additional message.

  • The custom filter returns the updated heading.

  • No PHP errors appear.

Expected result: You should have a working WordPress project demonstrating action hooks, filter hooks, callback functions, priorities, and custom hooks.

22. Frequently Asked Questions

Q1. What are WordPress Hooks?

WordPress Hooks are predefined connection points that allow developers to execute custom functions or modify data without directly changing WordPress core files.

Q2. How many main types of WordPress Hooks are there?

There are two main types: Action Hooks and Filter Hooks.

Q3. What is the difference between add_action() and add_filter()?

add_action() registers a callback to perform an operation at a specific point.

add_filter() registers a callback to modify and return data.

Q4. What is a callback function?

A callback is a PHP function registered with a hook. WordPress executes it when the associated hook runs.

Q5. What is hook priority?

Priority determines the order in which callbacks registered to the same hook execute. The default priority is 10.

Q6. What is accepted_args?

It specifies how many arguments a callback accepts from the corresponding action or filter invocation.

Q7. Can we create our own WordPress Hooks?

Yes. Developers can create custom action hooks using do_action() and custom filter hooks using apply_filters().

Q8. Can we remove an existing hook?

Yes. Use remove_action() or remove_filter() with the matching callback and priority.

Q9. Can WordPress Hooks be used inside plugins?

Yes. Hooks are widely used in WordPress plugins and themes to extend functionality.

Q10. Should I use hooks instead of editing WordPress core files?

Yes. Hooks are the recommended extension mechanism for many customizations because direct core modifications can be overwritten during updates and create maintenance problems.

23. Conclusion

WordPress Hooks are one of the most important concepts in WordPress development.

They allow developers to extend WordPress functionality without directly modifying core files.

In this tutorial, you learned:

  • What WordPress Hooks are.

  • The difference between Actions and Filters.

  • How callback functions work.

  • How to use add_action().

  • How to use add_filter().

  • How hook priority works.

  • How to use accepted arguments.

  • How to modify post titles and content.

  • How to create custom action and filter hooks.

  • How to remove registered callbacks.

  • How to troubleshoot common hook errors.

Final takeaway: If you want to become a professional WordPress developer, understanding Actions and Filters is essential. They provide the foundation for developing custom themes, plugins, and advanced WordPress functionality.

←
Post 34 of 40
→