0

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"?

1

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/

3
  • 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. Dec 12 '10 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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