Hook if somebody saves plugin options?

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…?

3 s
3

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);

Leave a Comment