After plugin options changed hook

i am looking for a hook that fires after the options for my plugin have changed.

I have an option named “interval”, it basically registers a cron schedule. Now, id like the change to take effect, right after the option is changed.

Read More

What hook can i hook in to for that? This is my settings page

add_action( 'admin_menu', 'apd_create_menu' );

function apd_create_menu() {

    add_menu_page('APD Plugin Settings', 'APD Settings', 'administrator', __FILE__, 'apd_settings_page',plugins_url('/images/icon.png', __FILE__));
    add_action( 'admin_init', 'register_apd_settings' );
}

function register_apd_settings() {
    //register our settings
    register_setting( 'apd-settings-group', 'directory' );
    register_setting( 'apd-settings-group', 'interval_time' );
    register_setting( 'apd-settings-group', 'interval_posts' );
}

EDIT:

add_filter( 'update_option_apd-settings-group', 'run_after_change', 10, 2 );

function run_after_change( $old_value, $new_value )
{
    echo $new_value . '<br>';
    echo $old_value;
    die();
}

Does not seem to have any effect when added

Related posts

1 comment

  1. Use the action update_option_{$option}, where $option is the same as the second argument for register_setting(). This hook fires after the option has been updated.

    add_filter( 'update_option_directory', 'run_after_change', 10, 2 );
    
    function run_after_change( $old_value, $new_value )
    {
        // compare both values and do something
    }
    

    10 is the priority, 2 the number of accepted arguments for the callback function.

    The opposite is pre_update_option_$option, which runs before the option is updated. It sends the same two parameters.

Comments are closed.