WordPress.org

Ready to get started?Download WordPress

Forums

Bug in apply_filters (5 posts)

  1. Davepar
    Member
    Posted 9 years ago #

    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.

    Dave

  2. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

  3. Davepar
    Member
    Posted 9 years ago #

    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.

  4. Davepar
    Member
    Posted 9 years ago #

    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.

  5. mani_monaj
    Member
    Posted 9 years ago #

    I ran to that problem too ,

    I wrote the pacth , too , but i found out in wp 1.5.1 everything solved look at http://www.cenamayo.com/wpxref/wp-includes/functions.php.source.html#l982

    :D

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.