Why do filters/actions require an argument count?

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.

Related posts

1 comment

    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()

Comments are closed.