[wp-hackers] Changing two variables listed in apply_filters hook

FUJINAKA Tohru tohru at nakarika.com
Sun Jul 22 12:30:01 GMT 2007


> When the apply_filters hook has 2 variables how 
> do you change the second variable in a plugin?

I think you have no chance while you do not change hook source's code.

Or, you can do but there's a limitation.

Here's a sample code.

--- [wordpress/test.php] ---
require './wp-config.php';

add_filter('test-me', 'earlier_filter',    9, 2);
add_filter('test-me', 'change_2nd_value', 10, 3);
add_filter('test-me', 'later_filter',     11, 2);

function earlier_filter($first, $second)
    printf("%-20s[in]: %-16s, %s\n", __FUNCTION__, $first, $second);
    return 'earlier';

function change_2nd_value($first, $second, $second_array)
    printf("%-20s[in]: %-16s, %s\n", __FUNCTION__, $first, $second);
    $second_array[0] = 'world';
    return 'change_2nd_value';

function later_filter($first, $second)
    printf("%-20s[in]: %-16s, %s\n", __FUNCTION__, $first, $second);
    return 'later';

$second = 'second';
$first = apply_filters('test-me', 'first', &$second, array(&$second));

printf("%-20s    : %-16s, %s\n", 'result', $first, $second);


earlier_filter      [in]: first           , second
change_2nd_value    [in]: earlier         , second
later_filter        [in]: change_2nd_value, second
result                  : later           , world

As you see, the second argument of hook source's variable is modified after
calling filters but not among filters. It's the limitation I mentioned

  FUJINAKA Tohru aka reedom <tohru at nakarika.com>

More information about the wp-hackers mailing list