How to preset a sidebar widget with default content via script?

You need to do this in two steps.

First create the actual widget. Find a widget ID that does not exist, and toss in an extra entry in (off the top of my head) the widget_text option, which corresponds to the text widget config.

Off the top of my head, it’ll look something like:

$ops = get_option('widget_text');
// find an $id that works...
$ops[$id] = array(
  'title' => 'foo',
  'text' => 'bar', // content?
);
update_option('widget_text', $ops);

Next, add it to your sidebar (again, off the top of my head):

$sidebars_widgets = get_option('sidebars_widgets');
$sidebars_widgets[$sidebar_id][] = ["widget_text-$id"];
update_option('sidebars_widgets', $sidebars_widgets);

I’m no longer 100% sure on the actual option names, but the general idea is as above.

Also, if you want extensive examples of sidebar manipulation (including heaps of inanities related to API changes related to WP upgrades), check the inc/upgrade.php file of my theme:

http://www.semiologic.com/software/sem-reloaded/

Or my Subscribe Me plugin:

http://www.semiologic.com/software/subscribe-me/

Leave a Comment