1

Consider the following code:

if(!class_exists('Update_Option_Error')){
    class Update_Option_Error
    {
        public function __construct()
        {
            ob_start();
            // This should trigger on page load
            add_action('init', array($this, 'init'));
        }

        public function init()
        {
            // Try to get the option 'test_option'
            $test_option = get_option('test_option');

            echo "<script>alert('Option is set to: $test_option');</script>"; //DEBUG

            // If 'get_option' returned false (Meaning the option was not created yet)
            if($test_option === false){
                echo "<script>alert('Option was not set');</script>"; //DEBUG

                // Create the option 'test_option' and set it to 'Foo'
                update_option('test_option', 'Foo');
                // Now try to get the option again (It should be 'Foo')
                $test_option = get_option('test_option');

                echo "<script>alert('Option is now set to: $test_option');</script>"; //DEBUG
            // If 'get_option' returned something other than false (Meaning the option was set to something)
            }else{
                // If 'test_option' is set to 'Foo'
                if($test_option === 'Foo'){
                    echo "<script>alert('Option is Foo');</script>"; //DEBUG

                    // Update the option's value to 'Bar'
                    update_option('test_option', 'Bar');
                    // Now try to get the option again (It should be 'Bar')
                    $test_option = get_option('test_option');

                    echo "<script>alert('Option is now set to: $test_option');</script>"; //DEBUG
                // If 'test_option' is set to 'Bar'
                }elseif($test_option === 'Bar'){
                    echo "<script>alert('Option is Bar');</script>"; //DEBUG

                    // Update the option's value to 'Foo'
                    update_option('test_option', 'Foo');
                    // Now try to get the option again (It should be 'Foo')
                    $test_option = get_option('test_option');

                    echo "<script>alert('Option is now set to: $test_option');</script>"; //DEBUG
                }
            }
        }
    }
    $update_option_error = new Update_Option_Error();
}

Here is a brief explanation of what it should do:

  • Create a new Plugin Class 'Update_Option_Error'
  • Call function 'init' on every WordPress 'init' action
  • Try to get the option 'test_option'
  • If the option was false (not created yet):
    • Create the option and set it to 'Foo'
  • If the option was set to 'Foo':
    • Set the option to 'Bar'
  • If the option was set to 'Bar':
    • Set the option to 'Foo'

However, it appears that when this plugin is activated, the value of the option 'test_option' is either not updated properly or not read properly. For example, if the page is loaded and the option is read as 'Foo', the following can happen:

  • The option is read as 'Foo' (alert('Option is set to: Foo') then alert('Option is Foo'))
  • The option then should be updated to 'Bar' (update_option('test_option', 'Bar');), However:
    • Sometimes results in alert('Option is now set to: Foo')
    • Sometimes results in alert('Option is now set to: Bar')
  • Either way, the next page refresh still reads the option as 'Foo'
  • If a new page is loaded, sometimes the option will correctly be updated to 'Bar'

Notes:

  • This occurs regardless of if the option is initially set to 'Foo' or 'Bar'
  • Removing all echo and ob_start() does not resolve the issue
  • There appears to be no pattern on whether the option is successfully set and read
  • WordPress Version is 4.8
  • Feel free to test the plugin yourself Here

Why is this occurring? What can be done to fix the functionality?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.