Brolga & Swan

SEO Services & More

The Difference Between Actions and Filters in WordPress

Quick Definitions

These simple and elegant definitions come directly from the WordPress code in wp-includes/plugin.php.

"Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur."

"Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen."

Differences - The Short Version

There is no difference; in WordPress an action is exactly the same as a filter.

The Proof

Look a little deeper into wp-includes/plugin.php and you'll find the following code:

function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { return add_filter($tag, $function_to_add, $priority, $accepted_args); }

Q.E.D.

From A Different Angle - The Purists View

There are purists, such as Otto, who believe that you should treat actions and filters as conceptually different and that this really matters - see his article on actions and filters for this view. I have a lot of respect for Otto's contributions to improving WordPress. That doesn't mean he's infallible, and in this case he's not. These features are exactly the same and treating them as different creates schisms in understanding that inhibit development. I believe that there was a time when there was an intention to make them different, but something happened - perhaps just organic growth - with the result that they ended up being the same.

In his article Otto cites a problem where a contact-page plugin created a problem because it used a filter to send an email. The actual problem was that it used a filter on 'the_content' to send the email. Instead it should have separated the display / submission of the form from its processing and used a filter on 'init' for this processing (including sending the email). Otto says this himself in the forum he referenced. I have examined the code and concur. To then say it was caused by performing an action in a filter is a leap, and in the wrong direction.

One of the fundamental ways that we learn about the world is comparing how two things are both alike and different from each other - this establishes boundaries. Actions and filters execute exactly the same code within WordPress and behave in exactly the same way ergo - from both white and black box perspectives - they are the same. To say otherwise is to deny simple, irrefutable logic and is, hence, a non-sense.

Put the soap-boxes away and write robust code, life's too short for purist non-sense.