How to pass additional parameter to wordpress filter?

Viewed 11153
add_filter('wp_list_pages_excludes', 'gr_wp_list_pages_excludes');

function gr_wp_list_pages_excludes($exclude_array) {    
    $id_array=$array('22');
    $exclude_array=array_merge($id_array, $exclude_array);
    return $exclude_array;
}

I'm a newbie to wordpress. The above code works fine. But I need to pass additional argument, say $mu_cust_arg to the function gr_wp_list_pages_excludes. How can I make use of it via apply_filters, or any other methods? Any help is appreciated.

Thanks in advance.

3 Answers

You can indeed add multiple arguments to a filter/action, you just need to tell WordPress how many arguments to expect

Example, which won't work:

add_filter('some_filter', function($argument_one, $argument_two) {
    // won't work
}); 

apply_filters('some_filter', 'foo', 'bar'); // won't work

It will fail with an error that too many arguments was provided.

Instead, you need to add this:

add_filter('some_filter', function($argument_one, $argument_two) {
    // works!
    $arugment_one; // foo
    $arugment_two; // bar
}, 10, 2);  // 2 == amount of arguments expected

apply_filters('some_filter', 'foo', 'bar'); 
Related