I wrote a plugin that has various options and in case the user saves this options a CSS file will be generated. My intention is that the CSS is only created when the options are saved and not when somebody visits the page and the CSS will be created every single time.

So I need a hook that fires when somebody saves the options. I tried it like this:

    public function __construct( $file ) {
        $this->options = get_option('clb_plugin_options');
        add_filter( 'pre_update_option_clb_plugin_options', array( $this, 'generate_options_css' ) );       
}

    public function generate_options_css() {
[doing all css related stuff)

        return $this->options;

}

But in this case it does not take any changes which I made in the form into consideration, as I just save the options in $this->options which are already there. So how can I do it to hook in after someone saves the plugin options...?

up vote 11 down vote accepted

Although I don't agree with your purpose, here the action hooks you may use (you have not shown us what you are using to save the options, so I can not say which one is better).

If you use add_option to save options:

add_option_{option_name}: Runs after a the option with name "option_name" has been added using add_option() function. For example, for the option with name "foo":

add_action('add_option_foo', function( $option_name, $option_value ) {
     //....
}, 10, 2);

add_option: Runs before an option gets added to the database. Example:

add_action('add_option', function( $option_name, $option_value ) {
     //....
}, 10, 2);

added_option: Runs after an option has been added. Example:

add_action('added_option', function( $option_name, $option_value ) {
     //....
}, 10, 2);

There are also analog actions for delete_option(): delete_option_{option_name}, delete_option and deleted_option

If you use update_option to save options:

(update_option can be used also to save/create new options)

update_option_{option_name}: Runs after the option with name "option_name" has been updated. For example, for the option with name "foo":

add_action('update_option_foo', function( $old_value, $value ) {
     //....
}, 10, 2);

update_option: Runs before an option gets updated. Example:

add_action('update_option', function( $option_name, $old_value, $value ) {
     //....
}, 10, 3);

updated_option: Runs after an an option has been updated. Example:

add_action('updated_option', function( $option_name, $old_value, $value ) {
     //....
}, 10, 3);
  • Good answer, however the parameters aren't in the right order. First is old value, next new value and option_name is last. Maybe they changed it over time. – berend May 30 '17 at 6:54
  • There are several actions hooks in the answer, which one are you talking about exactly? I've checked the docs and it seems you are wrong. See docs for update_option and updated_option hooks. – cybmeta May 30 '17 at 7:48
  • Run the script and you will see, I suppose the docs are wrong then as well, I suggested that they edit it already. Take a look -> berend.biz/debug.png – berend May 31 '17 at 7:00
  • You are mixing things. Your image shows the debug of the action update_option_{$option}, not the action update_option. So, the answer seems correct, review it again. Tank you anyway for the interest. – cybmeta May 31 '17 at 9:35

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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