WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to add some code to my plugin populate a specific sidebar (if it exists) with a text widget containing preset content.

Assuming that the sidebar I'm trying to preset is referenced by ID as "my-widget", how would I preset the content's of that widget with a text widget containing the text "Hello World"?

share|improve this question
up vote 1 down vote accepted

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:


Or my Subscribe Me plugin:


share|improve this answer
Thanks Denis. I always appreciate your generosity of sharing your code :) – Scott B Dec 12 '10 at 3:46
Denis, I've already created the widget with my theme functions.php. Its referenced by id "my-widget". Does this alter your first step? Or is your first step just creating an empty text widget to hold the content that will be added to the "my-widget" container? – Scott B Dec 12 '10 at 15:23
First step is to create the widget. If you've done that, ignore the one I suggested. Be sure var_dump() the stuff I mentioned to see how it works. – Denis de Bernardy Dec 12 '10 at 16:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.