The difference between action and filter in WordPress

Posts

If you are a long-time WordPress developer you will surely have already encountered these two tools (sometimes indispensable I would say).

If you just have your own path with WordPress, you probably had to deal with an action or a filter too, and maybe they have put you in trouble.

In this post, I’ll try to explain as clearly as possible what these two tools are, and what’s the difference between them.

What are action and filters in WordPress?

Action and Filter are two so-called “hooks”.

Practically the action and filter hooks allow us to modify or add functionality to the core of WordPress, but without modifying it directly.

Let’s say, for example, that we want to change the behavior of WordPress when publishing a post. If there were no Hooks, we would have to go directly to the core of WordPress to find the functions that regulate this event, and modify them as we please.

Not recommended for two main reasons:

  • Security: by directly modifying the files that keep WordPress up, we would inevitably affect the security of the entire system, making it unstable and insecure.
  • Updates: WordPress is updated regularly. If we modify core files, once updated our changes will be overwritten by the update itself.

For these reasons WordPress itself gives us the possibility to modify and/or add functionalities to the core, but without putting our hands on it. And this is possible through the Hooks action and filter.

Hooks are generally added to the functions.php file, but can also be used in a Plugin, for example.

Difference between Action and Filter

The difference is that action allows us to add functionality while filtering to edit variables.

Let’s go into detail and let’s give some examples:

Action

Action is the hook you have to use if you want to run some code at a certain time.

Let’s say you just want to send an email every time a post is published.

What you need to do is write a function that handles the sending of the email and tell WordPress to execute that function at the time of posting, and that’s where the hook action comes in handy.

add_action('publish_post','send_email_new_post');
function send_email_new_post() {
    $to = '[email protected]';
    $subject = 'New Post!';
    $message = 'A post was published!';
    
    mail($to, $subject, $message);
}

add_action allows us to tell WordPress what we want to do, in this case just use the hook action. publish_post is the when, that is when we want WordPress to execute our code, while send_email_new_post is the name of our function to send the mail.

We can extend a bit more add_action, adding the priority that the function must have and how many arguments you can pass to the function.

The priority is indicated by a number, the smaller the number, and the higher the priority of our function. The default value is 10.

Let’s expand our function a bit to send emails so that we can pass an argument to the function.
Let’s add for example the title of the published post as the subject of the email.

add_action('publish_post','send_email_new_post',5,1);
function send_email_new_post($post_ID) {
    $to = '[email protected]';
    $subject = get_post_field('post_title',$post_ID);
    $message = 'A post was published!';
    
    mail($to, $subject, $message);
}

Filter

Filter, on the other hand, allows us to modify variables.

Let’s say you want to modify the content of your posts, adding at the end of the text something like “written by me!”.
(why should you ever, but it’s just an example :D)

In this case, we don’t have to perform a function, but we have to modify a variable, which in this case is the content of the post.

That’s why the hook filter helps us. As for action, let’s see it in practice.

add_filter('the_content','add_my_string_to_content');
function 'add_my_string_to_content($content) {
    $content .= 'written by me!';
    return $content;
}

As you can see, unlike add_action, with add_filter we tell WordPress what to change and not when to perform a function.

In this case we are telling WordPress to modify the content of the post by adding our string.

Just like add_action you can add priority and number of arguments to add_filter.

As you can see, this is very simple. I hope that everything is clearer to you and that you can use these tools better and more consciously in your WordPress projects.


Photo by Samuel Zeller on Unsplash