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
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.