0

I am trying to pass multi-dimensional array using add_filter. I am able to pass single values like this:

add_filter('example_filter', 'my_example_filter_1' );
function my_example_filter_1( $array ){
    $array[]='tax1';
    return $array;
}
add_filter('example_filter', 'my_example_filter_2' );
function my_example_filter_2( $array ){
    $array[]='tax2';
    return $array;
}
add_filter('example_filter', 'my_example_filter_3' );
function my_example_filter_3( $array ){
    $array[]='tax3';
    return $array;
}

print_r( apply_filters( 'example_filter', array()) );

But if the array is of the following format, I am unable to achieve it.

function wte_fixed_starting_dates_license()
{
    $wp_travel_engine = get_option( 'wp_travel_engine_license' );
    $wte_fixed_departure_license_key = isset( $wp_travel_engine['wte_fixed_departure_dates_license_key'] ) ? esc_attr( $wp_travel_engine['wte_fixed_departure_dates_license_key'] ):'';// setup the updater
    $array = 
    array( 
        'version' => '1.0.0',       // current version number
        'license' => $wte_fixed_departure_license_key,  // license key (used get_option above to retrieve from DB)
        'item_id' => WTE_FIXED_DEPARTURE_ITEM_ID,   // id of this product in EDD
        'author'  => 'WP Travel Engine',  // author of this plugin
        'url'     => home_url()
    );
    return $array;
}
add_filter( 'wp_travel_engine_licenses', 'wte_fixed_starting_dates_license' );

I am passing another array using the same filter like this:

function wte_authorize_net_license($array)
{
    $wp_travel_engine = get_option( 'wp_travel_engine_license' );
    $wte_authorize_net_license_key = isset( $wp_travel_engine['wte_authorize_net_dates_license_key'] ) ? esc_attr( $wp_travel_engine['wte_authorize_net_dates_license_key'] ):'';// setup the updater
    $array = 
    array( 
        'version' => '1.0.0',       // current version number
        'license' => $wte_authorize_net_license_key,    // license key (used get_option above to retrieve from DB)
        'item_id' => WTE_FIXED_DEPARTURE_ITEM_ID,   // id of this product in EDD
        'author'  => 'WP Travel Engine',  // author of this plugin
        'url'     => home_url()
    );
    return $array;
}
add_filter( 'wp_travel_engine_licenses', 'wte_authorize_net_license' );

If I pass multiple arrays like this then I only get the later value in the array like this.

Array ( [version] => 1.0.0 [license] => [item_id] => 79 [author] => WP Travel Engine [url] => http://localhost/wordpress )

There should be two version, two license and other parameters as well when I print the array, right? Or am I missing something?

Any help would be highly appreciable. Thanks!

3
  • it seems to be exactly the value you return... so not sure what is the question here, unless I missed something Commented Feb 20, 2018 at 9:24
  • edit your question to show us the result you expect.
    – mmm
    Commented Feb 20, 2018 at 9:32
  • @MarkKaplun Thanks for replying everyone. I have edited my question.
    – saurav.rox
    Commented Feb 20, 2018 at 10:02

1 Answer 1

0

If you using add_filter you should recieve some params, filter than and return it back. If not you will lose previously defined data.

function wte_fixed_starting_dates_license($array) {

Also see array_merge function.

And your example returning right array. license is empty, but other elements is ok.

2
  • I am editing my question to add more in the code.
    – saurav.rox
    Commented Feb 20, 2018 at 9:58
  • IMO, you forgot to add the input array in the function definition. Something like function wte_fixed_starting_dates_license($array).
    – Beee
    Commented Feb 20, 2018 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.