您就快到了,除了每个小部件的设置与sidebars_widgets
选项,它只存储小部件“实例”。
尝试以下功能-您仍然需要使用register_sidebar
, 但它减轻了在这些边栏上预先注册小部件的痛苦。它还可以确保任何现有的小部件;在此过程中,它们的设置不会丢失。
/**
* Pre-configure and save a widget, designed for plugin and theme activation.
*
* @link http://wordpress.stackexchange.com/q/138242/1685
*
* @param string $sidebar The database name of the sidebar to add the widget to.
* @param string $name The database name of the widget.
* @param mixed $args The widget arguments (optional).
*/
function wpse_138242_pre_set_widget( $sidebar, $name, $args = array() ) {
if ( ! $sidebars = get_option( \'sidebars_widgets\' ) )
$sidebars = array();
// Create the sidebar if it doesn\'t exist.
if ( ! isset( $sidebars[ $sidebar ] ) )
$sidebars[ $sidebar ] = array();
// Check for existing saved widgets.
if ( $widget_opts = get_option( "widget_$name" ) ) {
// Get next insert id.
ksort( $widget_opts );
end( $widget_opts );
$insert_id = key( $widget_opts );
} else {
// None existing, start fresh.
$widget_opts = array( \'_multiwidget\' => 1 );
$insert_id = 0;
}
// Add our settings to the stack.
$widget_opts[ ++$insert_id ] = $args;
// Add our widget!
$sidebars[ $sidebar ][] = "$name-$insert_id";
update_option( \'sidebars_widgets\', $sidebars );
update_option( "widget_$name", $widget_opts );
}
在您的情况下:
wpse_138242_pre_set_widget( \'footer-left\', \'text\',
array(
\'title\' => \'Test1\',
\'text\' => \'Test 1 Test\',
\'filter\' => false,
)
);
wpse_138242_pre_set_widget( \'footer-right\', \'text\',
array(
\'title\' => \'Test2\',
\'text\' => \'Test 2 Test\',
\'filter\' => false,
)
);