8

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

2
  • Hi @Torben, do you know if the options are shared between users or are they users specific data? Thanks!
    – RickON
    Oct 20, 2021 at 15:02
  • You can use register_setting to hook in to validation function and there check values. Here is the code from my project. The details you can find here wordpress.stackexchange.com/a/404871/63233
    – fdrv
    Apr 17, 2022 at 4:49

3 Answers 3

20

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);
5
  • 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, 2017 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, 2017 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, 2017 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, 2017 at 9:35
  • and if you use the Options API to save the options? Oct 25, 2022 at 0:46
0

I experienced that update_option_{option_name} was not executing on follow up setting changes, only once.

In the end I resolved it by adding a generation script to add_options_page callback function. This way it is generated if the options page is just opened, but the overhead seems acceptable.

1
  • The hook is only executing when the specific setting is actually changed.
    – davey
    Oct 1, 2019 at 8:29
0

You can use register_setting to hook in to validation function and there check values. Here is the code from my project.

function gmbt_register_settings() {
    register_setting( 'gmbt_time_plugin_options', 'gmbt_time_plugin_options', 'gmbt_time_plugin_options_validate' );
    add_settings_section( 'gmbt_settings', 'Set Your Location Goolge Place ID', 'gmbt_plugin_section_text', 'gmbt_time_plugin' );

    add_settings_field( 'gmbt_plugin_setting_ggl_place_id', 'Google Place ID', 'gmbt_plugin_setting_ggl_place_id', 'gmbt_time_plugin', 'gmbt_settings' );
}
add_action( 'admin_init', 'gmbt_register_settings' );

function gmbt_plugin_section_text() {}

function gmbt_plugin_setting_ggl_place_id() {
    $options = get_option( 'gmbt_time_plugin_options' );
    echo "<input id='gmbt_plugin_setting_ggl_place_id' name='gmbt_time_plugin_options[ggl_place_id]' type='text' value='" . esc_attr( !empty($options['ggl_place_id']) ? $options['ggl_place_id'] : '' ) . "' />";
}

function gmbt_time_plugin_options_validate( $input ) {
    $options = get_option( 'gmbt_time_plugin_options' );
    $ggl_place_id = trim( $input['ggl_place_id'] );
    if($ggl_place_id != $options['ggl_place_id']) {
        delete_transient( 'gmb_business_hours' );
        wp_die('was deleted');
    }
    return $input;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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