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

Topic settings-api plugins Wordpress

Category Web


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

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.


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.