settings api - update_option_$option action not working as expected

admin2025-06-02  1

I have a custom admin page that uses the settings API. Form submits to options.php. After submission I am attempting to do something in the background. I am hooking into update_option_$option to do this. This works fine, except on first run. No matter what I've tried, I simply can't get the update_option_$option hook to run. Is this intended behavior?

function my_register_settings() {
     register_setting('my_settings_group', 'my_custom_option');
}
add_action('admin_init', 'my_register_settings');

function do_after_update($old, $new) {
     // Do the stuff here
}
add_action('update_option_my_custom_option','do_after_update', 10, 2);

the do_after_update() will only fire the second time and beyond. I found a work around by adding add_option('my_custom_option'); into my registration function, but that seems hacky.

I have a custom admin page that uses the settings API. Form submits to options.php. After submission I am attempting to do something in the background. I am hooking into update_option_$option to do this. This works fine, except on first run. No matter what I've tried, I simply can't get the update_option_$option hook to run. Is this intended behavior?

function my_register_settings() {
     register_setting('my_settings_group', 'my_custom_option');
}
add_action('admin_init', 'my_register_settings');

function do_after_update($old, $new) {
     // Do the stuff here
}
add_action('update_option_my_custom_option','do_after_update', 10, 2);

the do_after_update() will only fire the second time and beyond. I found a work around by adding add_option('my_custom_option'); into my registration function, but that seems hacky.

Share Improve this question asked Feb 28, 2019 at 20:13 idealbrandonidealbrandon 1532 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Looks like update_option_{$option} will only fire on updated options, ie, not brand-new ones.

Reading the code for update_option(), I see this:

/** This filter is documented in wp-includes/option.php */
if ( apply_filters( "default_option_{$option}", false, $option, false ) === $old_value ) {
    // Default setting for new options is 'yes'.
    if ( null === $autoload ) {
        $autoload = 'yes';
    }

    return add_option( $option, $value, '', $autoload );
}

... before the update_option_{$option} hook. Since your $old_value is unset, it's false, and so, since false === false, you're hitting the return add_option() code.

add_option() has its own, related action hook: add_option_{$option}. So you should be able to hook into that, too.

Your updated code:

function my_register_settings() {
     register_setting('my_settings_group', 'my_custom_option');
}
add_action('admin_init', 'my_register_settings');

function do_after_update($old, $new) {
     // Do the stuff here
}
add_action('update_option_my_custom_option','do_after_update', 10, 2);

function do_after_add( $new ) {
    // Do the stuff here
}
add_action( 'add_option_my_custom_option', 'do_after_add' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748853854a314270.html

最新回复(0)