I have an add_filter function for the auth_cookie_expiration hook. This hook accepts three parameters. However, I am interested in passing it more parameters. For example:

add_filter( 'auth_cookie_expiration', 'get_expiration', 10, 5 );

This would be possible with apply_filter, but the add_filter function is called once, which makes it throw an error:

PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function get_expiration(), 3 passed in ... and exactly 5 expected

I got around this using closures, but it seems like a completely ridiculous way to do this:

add_filter( 'auth_cookie_expiration', function() use ($param1, $param2) { return get_expiration(null, null, null, $param1, $param2); } , 10, 3 );

Is there a proper/more elegant way to make it accept additional parameters (even better, the params I want in place of the default ones)? Am I misunderstanding how add_filter is supposed to work?

For the sake of example, suppose get_expiration looks like this:

function get_expiration( $length, $user_id, $remember, $param1, $param2 )
{
    return $param1 + $param2;
}

3 s
3

The second parameter in add_filter is a function with accepted arguments, not returned values.

This is an example how I pass my custom array $args to change an existing array $filter_args:

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', function( $filter_args ) use ( $args ) {
        return eswc_var_dropdown_args( $filter_args, $args );
    }
);

function eswc_var_dropdown_args( $filter_args, $args ) {
    $filter_args['show_option_none'] = $args['var_select_text'];
    return $filter_args;
}

Leave a Reply

Your email address will not be published. Required fields are marked *