Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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;
}
share|improve this answer
    
Just make sure to call apply_filters at an appropriate time. –  still_learning Mar 21 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 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 at 21:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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