$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>settings api - update_option_$option action not working as expected|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

settings api - update_option_$option action not working as expected

matteradmin10PV0评论

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' );
Post a comment

comment list (0)

  1. No comments so far