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')), 
        ) );
  • 1
    The code in that linked question will work if you think it through. All of the pieces are there.
    – s_ha_dum
    Commented Mar 17, 2014 at 15:28

1 Answer 1


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.

 * Pre-configure and save a widget, designed for plugin and theme activation.
 * @link    http://wordpress.stackexchange.com/q/138242/1685
 * @param   string  $sidebar    The database name of the sidebar to add the widget to.
 * @param   string  $name       The database name of the widget.
 * @param   mixed   $args       The widget arguments (optional).
function wpse_138242_pre_set_widget( $sidebar, $name, $args = array() ) {
    if ( ! $sidebars = get_option( 'sidebars_widgets' ) )
        $sidebars = array();

    // Create the sidebar if it doesn't exist.
    if ( ! isset( $sidebars[ $sidebar ] ) )
        $sidebars[ $sidebar ] = array();

    // Check for existing saved widgets.
    if ( $widget_opts = get_option( "widget_$name" ) ) {
        // Get next insert id.
        ksort( $widget_opts );
        end( $widget_opts );
        $insert_id = key( $widget_opts );
    } else {
        // None existing, start fresh.
        $widget_opts = array( '_multiwidget' => 1 );
        $insert_id = 0;

    // Add our settings to the stack.
    $widget_opts[ ++$insert_id ] = $args;
    // Add our widget!
    $sidebars[ $sidebar ][] = "$name-$insert_id";

    update_option( 'sidebars_widgets', $sidebars );
    update_option( "widget_$name", $widget_opts );

In your case:

wpse_138242_pre_set_widget( 'footer-left', 'text',
        'title' => 'Test1',
        'text' => 'Test 1 Test',
        'filter' => false,

wpse_138242_pre_set_widget( 'footer-right', 'text',
        'title' => 'Test2',
        'text' => 'Test 2 Test',
        'filter' => false,
  • Thank you. This works, but something isn't working right with the code that is supposed to see if the widget already exists. I have this firing in my widgets_init() hook and every time I go into the theme options area it re-adds the widgets. Actually, it appears to be doubling them. So first there were two, then four, then 8, etc. Commented Mar 17, 2014 at 18:05
  • Actually, I take that back. I just tried it again and it is only adding one at a time rather than doubling it. Commented Mar 17, 2014 at 18:10
  • As mentioned in my answer, this is designed for theme & plugin activation - if you fire it on widgets_init of course it'll keep adding them! Commented Mar 18, 2014 at 10:31
  • For actual moment this function do not update sidebars_widgets option in database at all. After installing and activation of theme, in database option sidebars_widgets have all of my registered sidebars from functions.php, but then, when i execute this function in my script, database entry sidebars_widgets won't to be updated. There is no problem with update widget_$name option. Options from $args are stored there but I noticed that this function do not add for ex. i:2;a:4: but only a:4. I think this funciton should be corrected somehow.
    – X9DESIGN
    Commented May 24, 2023 at 21:05

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.