Support » Requests and Feedback » Bug in apply_filters

  • I’m working on a list_cats plug-in. (Cool plug-in architecture, btw.) It appears that apply_filters is supposed to pass a string to the plug-in followed by additional arguments. In the case of list_cats, it would be nice to have the $category object with the cat_ID.

    However, apply_filters has two bugs that prevent this mechanism from working. First, it’s not grabbing the right args. the line:
    $args = array_slice(func_get_args(), 3);
    needs to be
    $args = array_slice(func_get_args(), 2);
    And secondly, the plus/union array operator is not working correctly since both arrays are indexed from zero. This line:
    $string = call_user_func_array($function, array($string) + $args);
    needs to be:
    $string = call_user_func_array($function, array_merge(array($string), $args));

    After those changes, I’m now getting the $category object in my plug-in and it’s working very nicely. However, now I’m getting a “Warning: Wrong parameter count for trim()” error from the call_user_func_array. Since WP supports adding arbitrary functions to the filter/action list, those extra args might cause problems.


Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Ryan Boren


    WordPress Dev

    I just figured out a hack that gets around the issue. Replace the call to call_user_func_array() in apply_filters with this:
    if ($function == 'trim') {
    $string = call_user_func_array($function, array($string));
    else {
    $string = call_user_func_array($function, array_merge(array($string), $args));

    A more correct (although slower) solution would be to call get_defined_functions() and pass just the one parameter if the $function is in the ‘internal’ part of the array.

    do_action seems to have a similar problem where the array union operator is being used.

    Ah, well I wish I had seen that bug this morning. Could have saved some time.

    There is one more weird issue where even with the fix, my plugin is still called once with only one arg. Maybe it’s a bug specific to list_cats plug-ins. I’ll try to track it down.

    I ran to that problem too ,

    I wrote the pacth , too , but i found out in wp 1.5.1 everything solved look at


Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Bug in apply_filters’ is closed to new replies.