How to pass additional parameter to wordpress filter?

Because WP doesn't accept closures as callbacks (at least, certainly not for add_filter()) the short answer is "you can't". At least, not in a tidy way.

There are a couple of options here, depending on what you are doing. The first is the best, but you may not be able to use it:

Write a wrapper function that calls your function:

function gr_wp_list_pages_excludes_1 ($exclude_array) {
  $custom_arg = 'whatever';
  gr_wp_list_pages_excludes_1($exclude_array, $custom_arg)
}

This will only work if you are always passing the same custom argument in a given situation - you would write one of these wrapper functions for each different situation, and pass the name of the wrapper function to add_filter(). Alternatively, if you want it to be truly dynamic, you would need to...

Use a global variable: (Ref: Variable scope, $GLOBALS)

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

Using this approach means that you can pass any data you like into the function by assigning it to $gr_wp_list_pages_excludes_custom_arg in the global scope. This is generally regarded as bad practice and heavily frowned upon, because it makes for messy and unreadable code and leaves the memory space littered with extra variables. Note that I have made the variable name very long and specific to the function to avoid collisions - another problem with using global variables. While this will work, only use it if you absolutely have to.


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'); 

Very simple!

add_filter('filter_name','my_func',10,3);     //three parameters lets say..
my_func($first,$second,$third){
  //............
}

then

echo apply_filters('filter_name',$a,$b,$c);