0

I created an array with some default settings in a plugin, and want to allow it to be replaced from functions.php in a theme without using stored data (eg. add_option). I want this to be just a hook with new values.

example array inside plugin:

$default_settings = array(
   'key_one' => 0,
   'key_two' => 1
);

I need to allow this to be changed from a theme's functions.php, so a developer can add something like this:

add_filter('the_hook','my_function');
function my_function(){
  $default_settings = array(
       'key_one' => 1, //changed value
       'key_two' => 1
  );
}

If a developer makes this call, then I want the new values replacing the old array inside the plugin. Any help is appreciated.

1

You are already pretty close. Extending your example:

In your plugin put this:

$default_settings = array(
   'key_one' => 0,
   'key_two' => 1
);
$settings = apply_filters( 'example_filter', $default_settings);

Then a theme can change it like this:

add_filter('example_filter','my_function');
function my_function($settings){
   $settings['key_one'] = 1;
   return $settings;
}
  • Just make sure to call apply_filters at an appropriate time. – still_learning Mar 21 '14 at 21:00
  • thanks for this, I do believe I have to call at a time like still_learning mentions, I can't see the new results. I added print_r($settings); right after $settings = apply_filters.... but I'm still getting the old values. Example: Array ( [key_one] => 0 [key_two] => 1 ) – Alex Pena Mar 21 '14 at 21:37
  • sorry, just figured out what i had to do, thanks. I added this into a new function, then an action with init. Works now. – Alex Pena Mar 21 '14 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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