Support » Developing with WordPress » update_options most always setting the value to null

  • Resolved sydream

    (@sydream)


    I’m working on a small plug-in where I’m facing a little issue I can’t wrap my head around.

    I’m using the update_option function to update an option I’ve previously created via register_setting. Despite the function being passed the correct parameters, it keeps setting that option to NULL instead.

    I’ve enabled the SAVEQUERIES flag so to look at the SQL queries generated upon using the update_option function and I can see that despite the second parameter being passed having a non NULL value, the query keep resulting in a SET to NULL.

    I’ve also printed out a few debug information just to see what’s happening and I can’t explain where the problem is. There is only one field/option that this works fine with, but why it’s a mystery to me (it is a number, but I have other numeric options that don’t work as well)

    This is the code that uses update_option

    
     foreach($settings as $field) {
    $res = update_option( $field['uid'], $field['default']);
                    echo '<pre>resetting: '; print_r($field['uid']); echo '</pre>';
                    echo '<pre>to: '; print_r($field['default']); echo '</pre>';
                    echo '<pre>res: '; print_r($res); echo '</pre>';
                    echo '<pre>reading back: '; print_r(get_option($field['uid'])); echo '</pre>';     
                    echo '<pre>query: '; print_r($wpdb->queries[sizeof($wpdb->queries)-1]); echo '</pre>';
    }

    where $settings it’s set like this (just an extract)

    $settings = array (            
                array(
                    'uid'           => 'myplugin_image_max_width',
                    'default'       => '1920'),
                array(
                    'uid'           => 'myplugin_cache_lifetime',
                    'default'       => DAY_IN_SECONDS))

    The output I get is this, the first doesn’t work and set the value to NULL even though the default value is ‘1920’, while the latter works just fine (and that’s the only one that does).

    resetting: myplugin_image_max_width
    to: 1920
    res: 
    reading back: 
    query: Array
    (
        [0] => UPDATE <code>wp_options</code> SET <code>option_value</code> = NULL WHERE <code>option_name</code> = 'myplugin_image_max_width'
        [1] => 0.0047221183776855
        [2] => update_option, sanitize_option, apply_filters('sanitize_option_myplugin_aspect_ratio'), WP_Hook->apply_filters, MYPLUGIN_Admin->sanitize_me, MYPLUGIN_Settings::set_defaults, update_option
        [3] => 1623282203.9853
        [4] => Array
            (
            )
    
    )
    
    resetting: myplugin_cache_lifetime
    to: 86400
    res: 1
    reading back: 86400
    query: Array
    (
        [0] => UPDATE <code>wp_options</code> SET <code>option_value</code> = '86400' WHERE <code>option_name</code> = 'myplugin_cache_lifetime'
        [1] => 0.0022330284118652
        [2] => update_option, sanitize_option, apply_filters('sanitize_option_myplugin_aspect_ratio'), WP_Hook->apply_filters, MYPLUGIN_Admin->sanitize_me, MYPLUGIN_Settings::set_defaults, update_option
        [3] => 1623282204.0232
        [4] => Array
            (
            )
    
    )

    I’ve also read about this bug, so not sure if this is what’s affecting me but would really not touch the wp basecode.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter sydream

    (@sydream)

    Never mind, I’ve found the issue. I have a custom sanitize callback that is called as part of the data saving process, this method was nullifying almost everything.

    The only parameter working was due to using a different sanitize method, which didn’t have bugs.

    I only found out by looking at the call trace, and realizing that the issue was in the sanitize callback.

    Moderator bcworkz

    (@bcworkz)

    This correctly saves values on my site:

    $settings = array (            
                array(
                    'uid'           => 'myplugin_image_max_width',
                    'default'       => '1920'),
                array(
                    'uid'           => 'myplugin_cache_lifetime',
                    'default'       => DAY_IN_SECONDS),
                );
                
    foreach($settings as $field) {
    	$res = update_option( $field['uid'], $field['default']);
    }

    Executed from a page template file.

    Using get_option() immediately after update_option() to confirm is unreliable due to a likely race condition. Code executes faster than the DB can write the value. I verified the saved values manually via phpMyAdmin.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.