2

While working on a project I used the incorrect hook type add_action() which does not exist, yet there was no error notice and the correct result returned. I only realized it was wrong while searching for the method along with the $hook_name name in the package and it was not found, and when I checked the code reference page, it showed that it is add_filter().

Why does it still work?

1 Answer 1

6

add_action() uses add_filter() behind the hood.

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

Source: https://developer.wordpress.org/reference/functions/add_action/

So basically it doesn't matter if you use add_filter or add_action.
But it doesn't mean you should, using the appropriate function for a event will make your code easier to follow and understand, if you always use add_filter or add_action for every single event it can create a lot of confusion.

1
  • 4
    Case in point: filters should always return a value. If you were to use add_action instead of add_filter it would not be clear from your code that a return value was required.
    – vancoder
    May 12, 2022 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.