2

I was just looking at plugin.php trying to answer the above question and I couldn't work it out. The actual code that calls filters is this:

do {
    foreach( (array) current($wp_filter[$tag]) as $the_ )
        if ( !is_null($the_['function']) ){
            $args[1] = $value;
            $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
        }

} while ( next($wp_filter[$tag]) !== false );

If (int) $the_['accepted_args'] wasn't there then it would simply pass everything available to the target function without any adverse affects for functions with less formal params.

1
  • Note that function accepting multiple parameters doesn't mean you want filter to fill them all in. In some edge cases you even want 0 to prevent filter from passing any parameters. That gives more flexibility to use functions in filters without writing special versions/wrappers of them just for that.
    – Rarst
    May 29, 2013 at 11:58

1 Answer 1

4
  1. Because of backwards compatibility.

    You can use the same callback for multiple filters. Inside of that callback you should use current_filter() to determine the context. But some plugins use the number of passed arguments instead. Changing that would break these plugins.

    That’s why you should always use the API (here: current_filter()) and not some made-up construct.

  2. Also, PHP will raise a warning when you pass more parameters to a native function than that function expects.

See Ticket #14671 Deprecate the "accepted args" argument in add_filter() and add_action()

Your Answer

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

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