Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

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:

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

Or my Subscribe Me plugin:

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

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

Your Answer

 
discard

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.