I’m trying to add a text widget with the title and text set, to two custom sidebars in my custom theme. But, I can’t find a clear walkthrough of how to do it. I’ve seen bits of code here and there, but nothing that clearly walks through the process of programatically adding the text widget to a sidebar.
I’ve seen this but it revolves around a custom widget which is not what I’m trying to do.
Edit:
I have two sidebars – footer-left and footer-right. Here’s what I have tried but it has not worked. So, can anyone tell me what I’m doing wrong?
update_option( 'sidebars_widgets',
array (
'footer-left' => array ( 0 => array( 'title' => 'Test1', 'text' => 'Test 1 Test')),
'footer-right' => array ( 0 => array( 'title' => 'Test2', 'text' => 'Test 2 Test')),
) );
You’re almost there, except each widget’s settings are stored separately from the
sidebars_widgets
option, which just stores widget “instances”.Try out the following function – you’ll still need to use
register_sidebar
, but it eases the pain for then pre-registering widgets on those sidebars. What it also does is ensure that any existing widgets & their settings aren’t lost in the process.In your case: