1

I am working with redux framework and need to save theme option values to a file right before it is saved in DB. Is there a way to intercept

update_option()

right before it saves data in DB?

2

You might be better off using the update_option_ action instead. This will allow you to write to your file after the option has been updated (as apposed to before). If another plugin hooks into pre_update_option_ and alters the value after your plugin has saved it then you will have saved an incorrect value.

I would use:

add_action( 'update_option_myoption', function( $old_value, $new_value ) {
   //Do something with the new value
}, 10, 2);
1
  • Very good point. – cybmeta Mar 18 '15 at 8:37
4

There is a filter exactly for that: pre_update_option_{option_name}. If the is option is, for example, names myoption, you could use somthing like this:

add_filter( 'pre_update_option_myoption', function( $new_value, $old_value ) {

   //Do something before returning the new value to be saved in database

   return $new_value;

}, 10, 2);

Referene: pre_update_option_(option_name)

It would be useful a general pre_update_option for all options, but as far I know such filter doesn't exist.

You asked about someting to use right before the option is saved to DB but you may be also interested in update_option_{option_name} action. This action is fired right after the option has been saved. Please, note that update_option_{option_name} is an action while pre_update_option_{option_name} is a filter, both are intented for different things. Basically (very basically), actions are intended to perform tasks and don't return values; filters return values and they are intented to work with or modify data (although there is nothing that stop you to perform tasks in filters).

0

Lead dev of Redux here. You can use one of our filters to match your needs:

"redux/validate/{$this->args['opt_name']}/defaults" - Only on resetting defaults

"redux/validate/{$this->args['opt_name']}/defaults_section" - Only on resetting one section

"redux/options/{$this->args['opt_name']}/import" - Only on import

"redux/validate/{$this->args['opt_name']}/before_validation" - before validation

"redux/options/{$this->args['opt_name']}/validate" - after validation

2

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.