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

This plugin is a simple demo test to see if I can dynamically populate a pre-existing widget area with text. In this case, my theme has created the widget area "home-header-widget" and I'm trying to populate it with the contents of 'text' below. I'm stuck at the line...

$sidebars_widgets[$sidebar_id] = "widget_text-".$id;

In that I'm not certain how to obtain a reference to the dynamically created text widget object in order to preset its contents and place it inside my 'home-header-widget'...

Plugin Name: Widget Test
function cb_activate_widgettest(){

$sidebar_id = 'home-header-widget';
$sidebars_widgets = get_option('sidebars_widgets');
$id = count($opts)+1;
$sidebars_widgets[$sidebar_id] = array("text-".$id);

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


register_activation_hook(__FILE__, 'cb_activate_widgettest');
share|improve this question
up vote 2 down vote accepted

There are a few mistakes in your code:

  1. 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.

  2. 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);

share|improve this answer
@OTP: That did it. Thanks for your help. I've updated my question with the corrected code. – Scott B Mar 13 '11 at 2:58

How to preset a sidebar widget with default content via script?
How to add multiple copies of a widget from "available widgets"
Code to dynamically populate a widget with preset content

This is your fourth topic on widget setup, could this not have been addressed inline with any of your other widget topics?

I feel these widget topics are starting to become a little duplicative.

share|improve this answer

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.