There are a few mistakes in your code:
-
You need to specify the instance ID manually, for eg. 2. If you want to append a text widget, and not overwrite the sidebar’s contents, then try using
$id = count($opts)+1
, or you could just generate a random ID. -
Use the widget_ID_base-instance_ID in
$sidebars_widgets[$sidebar_id] = "widget_text-".$id;
, and put that inside a array:$sidebars_widgets[$sidebar_id] = array("text-".$id);