Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass additional parameter to wordpress filter?

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.

like image 753
joHN Avatar asked Aug 01 '12 06:08

joHN


2 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'); 
like image 152
FooBar Avatar answered Oct 02 '22 18:10

FooBar


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.

like image 30
DaveRandom Avatar answered Oct 02 '22 16:10

DaveRandom