Apparently, callbacks to the WordPress function add_filter() don’t receive more than one argument to their callback unless you specify the fourth argument to the add call as a number greater than 1. I didn’t really notice the docs mention that. It took me quite some time var_dump()ing from core code to figure this out.

It doesn’t really make sense to me that the fourth argument defaults to 1 rather than all arguments. The core code just calls call_user_func_array() with an array of the arguments passed by the apply_filter() call. That will happily allow more arguments than a function can take. Sure, you might have cases where a callable that is used elsewhere has some arguments (with defaults) you don’t want passed from the filter, but that seems like the rarer case. You’d likely know that you needed something special and, if you didn’t know about the fourth argument, look up a way to deal with it.

The way things are just makes it confusing for a newcomer (or long-time but occasional user) adding a simple dedicated callable for a filter who looks up the arguments it’s supposed to receive and wonders why it’s not receiving more than one.

#Page top