diff --git a/src/wp-includes/class-wp-widget.php b/src/wp-includes/class-wp-widget.php index 2ccbbcf937..17f79385ce 100644 --- a/src/wp-includes/class-wp-widget.php +++ b/src/wp-includes/class-wp-widget.php @@ -539,8 +539,13 @@ class WP_Widget { $settings = get_option( $this->option_name ); - if ( false === $settings && isset( $this->alt_option_name ) ) { - $settings = get_option( $this->alt_option_name ); + if ( false === $settings ) { + if ( isset( $this->alt_option_name ) ) { + $settings = get_option( $this->alt_option_name ); + } else { + // Save an option so it can be autoloaded next time. + $this->save_settings( array() ); + } } if ( ! is_array( $settings ) && ! ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) ) { diff --git a/tests/phpunit/tests/widgets.php b/tests/phpunit/tests/widgets.php index bf67894477..a98942044c 100644 --- a/tests/phpunit/tests/widgets.php +++ b/tests/phpunit/tests/widgets.php @@ -465,6 +465,11 @@ class Tests_Widgets extends WP_UnitTestCase { $this->assertArrayHasKey( 'title', $instance ); unset( $option_value['_multiwidget'] ); + // Pretend this widget is new. + delete_option( 'widget_nav_menu' ); + $never_used = get_option( 'widget_nav_menu' ); + $this->assertFalse( $never_used ); + wp_widgets_init(); $wp_widget_search = $wp_registered_widgets['search-2']['callback'][0]; @@ -475,6 +480,11 @@ class Tests_Widgets extends WP_UnitTestCase { foreach ( $option_value as $widget_number => $instance ) { $this->assertEquals( $settings[ $widget_number ], $option_value[ $widget_number ] ); } + + // After widgets_init(), get_settings() should create the widget option. + $never_used = get_option( 'widget_nav_menu' ); + $this->assertEquals( 1, $never_used['_multiwidget'] ); + $this->assertArrayNotHasKey( 0, $never_used ); } /**