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');

2 Answers 2


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

  • @OTP: That did it. Thanks for your help. I've updated my question with the corrected code.
    – Scott B
    Commented Mar 13, 2011 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"

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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