plugins - merging an array to an existing array using add_filter

I'm working on a plugin and i want to be able to extend some of the functionality using apply_filters(). I have a s

I'm working on a plugin and i want to be able to extend some of the functionality using apply_filters(). I have a setup like this which is the default values;

print_r( apply_filters( 'some_example_hook', array(
    'Hello1'    =>  'HELL0',
    'Hello2',   =>  'HELL1',
    'Hello3',   =>  'HELL2',
    'Hello4',   =>  'HELL3',
) ) );

Now i want to be able to add to the array above (so i can extend the default values) but each time i create a new function and add_filter, it overrides everything in the array with this new array. This is what i mean;

add_filter( 'some_example_hook', 'add_yourself_to_some_examplehook' );
function add_yourself_to_some_examplehook($args) {
    $args = array(
        'Hello5'    => 'HELL4'
    ); 
    return $args;
}

the output will become;

Array ( [Hello5] => HELL4 )

instead of;

Array ( 
    [Hello1] => HELL0,
    [Hello2] => HELL1,
    [Hello3] => HELL2,
    [Hello4] => HELL3,
    [Hello5] => HELL4,
)

So that i can easily do something like this with the arrays anywhere and whenever an array is added to the filter (as we did above from anywhere or any plugin, it should just add the array to the filter and make the code below valid;

if ( in_array('HELL4', apply_filters( 'some_example_hook', array(
    'Hello1'    =>  'HELL0',
    'Hello2',   =>  'HELL1',
    'Hello3',   =>  'HELL2',
    'Hello4',   =>  'HELL3',
  ) ) ) ) {
       //Do Something
       // THIS WILL BE VALID because we have added it using add_filter above
 }

I have tried this method here Insert new element to array with add_filter but the output is just this Array ( [Hello5] => HELL4 ) ... it is not appending the array to the existing array. Not sure what i am doing wrong.

Thanks in advance for your help

I'm working on a plugin and i want to be able to extend some of the functionality using apply_filters(). I have a setup like this which is the default values;

print_r( apply_filters( 'some_example_hook', array(
    'Hello1'    =>  'HELL0',
    'Hello2',   =>  'HELL1',
    'Hello3',   =>  'HELL2',
    'Hello4',   =>  'HELL3',
) ) );

Now i want to be able to add to the array above (so i can extend the default values) but each time i create a new function and add_filter, it overrides everything in the array with this new array. This is what i mean;

add_filter( 'some_example_hook', 'add_yourself_to_some_examplehook' );
function add_yourself_to_some_examplehook($args) {
    $args = array(
        'Hello5'    => 'HELL4'
    ); 
    return $args;
}

the output will become;

Array ( [Hello5] => HELL4 )

instead of;

Array ( 
    [Hello1] => HELL0,
    [Hello2] => HELL1,
    [Hello3] => HELL2,
    [Hello4] => HELL3,
    [Hello5] => HELL4,
)

So that i can easily do something like this with the arrays anywhere and whenever an array is added to the filter (as we did above from anywhere or any plugin, it should just add the array to the filter and make the code below valid;

if ( in_array('HELL4', apply_filters( 'some_example_hook', array(
    'Hello1'    =>  'HELL0',
    'Hello2',   =>  'HELL1',
    'Hello3',   =>  'HELL2',
    'Hello4',   =>  'HELL3',
  ) ) ) ) {
       //Do Something
       // THIS WILL BE VALID because we have added it using add_filter above
 }

I have tried this method here Insert new element to array with add_filter but the output is just this Array ( [Hello5] => HELL4 ) ... it is not appending the array to the existing array. Not sure what i am doing wrong.

Thanks in advance for your help

Share Improve this question edited Sep 6, 2019 at 17:20 Kolawole Emmanuel Izzy asked Sep 6, 2019 at 16:58 Kolawole Emmanuel IzzyKolawole Emmanuel Izzy 2031 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You are overwriting $args in add_yourself_to_some_examplehook(). Based on what you're saying you want to achieve, you should be appending the new item instead:

add_filter( 'some_example_hook', 'add_yourself_to_some_examplehook' );
function add_yourself_to_some_examplehook($args) {
    $args[ 'Hello5' ] = 'HELL4'; // <-- Note the syntax here.

    return $args;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745179133a4615337.html

相关推荐

  • plugins - merging an array to an existing array using add_filter

    I'm working on a plugin and i want to be able to extend some of the functionality using apply_filters(). I have a s

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信