1

I read the action hook is use for call the functions and filter hook is use for filtering the content before save or display on website. Can you tell me the some working phases of project example where we use action and filter hook. So it will better to understand more.

ketan sharma
  • 9
  • 1
  • 4

3 Answers3

4

Action: Doing new things. Ex. adding CSS and JS.

Filter: Doing something which is already in WordPress. Adding or removing some things. Assume adding some text after post content. It means doing something with WordPress.

Example code:

add_action('admin_menu','my_func',8,2);
function my_func($one, $two){
    //Something here …
}
Community
  • 1
  • 1
3

You want to know what the difference is between an action and filter event (hook).

Let's start with understanding what a hook or event is in WordPress.

What is a Hook (Event)?

Through the Plugin API, WordPress provides us with the means to run our code when events happen.

Programs run in a specific order from top to bottom. Line 1 runs and then line 2 and so on. As WordPress loads in files, each of those run and so on.

As the web page request is processed, WordPress Core is running the lines of code and doing stuff. But what if you need to run your code at a certain point in the request cycle? For example, let's say you want to change the post's byline from the default of the theme. Using a filter event (hook), you gain:

  • access to the actual value or HTML before it's sent out to the browser
  • the ability to change that value or HTML
  • the ability to run other code at a specific point in the web page request cycle.

Why are there hooks (events)?

WordPress runs out of the box without our custom themes or plugins. But the Plugin API gives you and I as well as all developers the means to customize, change the default behavior, and extend WordPress.

It lets us run our code when we need to, i.e. at specific points in the web page request cycle.

Pre-registration

For our code to run, we need to pre-register our callback, which is the function or method that we want to run at that specific point. You use add_action() to pre-register an action event callback. You use add_filter() to pre-register a filter event callback.

When you pre-register your callback, it's being added into a registration lookup table, which is an array.

Then when the event fires, your callback and all the others are called in order. You set the order using the priority parameter. The argument(s) are passed to your callback. Then your method or function runs.

Here's a video that explains the big picture of this process and the why of it.

Action vs. Filter

A filter event (hook) allows you to filter the return value. It gives you (and all the other registered callbacks) the ability to filter, which means change the value.

Think about that. Let's say you want to change the "read more" link. Maybe you want it to say "Continue reading" and maybe you want it to be a button instead of a just a hyperlink. The filter allows you:

  • to receive the current value
  • change it to what you want
  • send it back by returning it

That new value is then passed along to all the other registered callbacks and then eventually returned to the line of code that fired the event.

Action Event

The action event (hook) is the same as a filter except that the value is not returned.

The event is fired, such as init or plugins_loaded. The pre-registered callbacks are called in order and any arguments are passed to each one. Your function or method is called. You can run your code.

It gives you the means to do something at a specific point in the web page request cycle.

Wrap it Up

Think about the power of the event management system. If you need to run something say right after all of the plugins have loaded, you pre-register to the plugins_loaded event name. When Core fires the event by doing do_action( 'plugins_loaded' );, your code will run too.

Filter and Action events (hooks) let you run your code at a specific point in the web page request cycle.

The Difference

Filter events (hooks) let you filter or change the value before it's processed. Action events do not let filter.

How do you fire an event?

For action events, you can use do_action( 'event_name' ) or do_action_ref_array( 'event_name', $args ).

For filter events, you can use apply_filters( 'event_name', $value_to_filter ) or apply_filters( 'event_name', $args );

Examples

Let's say you want to add a styling class attribute to the post. You can pre-register to the post_class event name. You'll receive an array of classes.

add_filter( 'post_class', 'add_my_class_attribute_to_post_class' );
/**
 * Add styling class to the post class.
 *
 * @since 1.0.0
 *
 * @param array $classes
 *
 * @return array
 */
function add_my_class_attribute_to_post_class( array $classes ) {
    $classes[] = 'some-class-attribute'; 

    return $classes;
}

What if you wanted to add a styling class to the front page's body.

add_filter( 'body_class', 'add_front_page_to_body_class' );
/**
 * Add the class attribute "front-page" to the body classes.
 *
 * @since 1.0.0
 *
 * @param array $classes
 *
 * @return array
 */
function add_front_page_to_body_class( array $classes ) {
    $classes[] = 'front-page';

    return $classes;
}

Notice how you're changing the value and then returning it back.

hellofromTonya
  • 1,301
  • 8
  • 8
2

In short:

Filter Hook: used to modify (filter) a value

Action Hook: do something, right at this moment


Filter Hook Example:

$name = "Smith";
$gender = "female";

echo apply_filter('form_of_address', $name, $gender );



// somewhere else
add_filter( 'form_of_address', function( $name, $gender ) {
    
    if ( "female" == $gender ) {
        $name = "Mrs. " . $name;
    } else if ( "male" == $gender ) {
        $name = "Mr. " . $name;
    }
    
    return $name
    
}, 10, 2 );
Community
  • 1
  • 1
Andy Tschiersch
  • 3,716
  • 1
  • 17
  • 24