How do i place same widgets multiple time by default on theme activation?

I am creating a portfolio theme and want to place some widgets on sidebar by default on theme activation. I did it but the problem is that, I want to place Text widget two times at the same sidebar say at primary sidebar but somehow my 2nd widget is not saved/ placed.

What should I do?

$sidebars_widgets = array();
$text = array();

$text[0] = array( "title"         =>    'Special Offers',
    "text"        =>    'Super Discount offers for everyone. <strong>25% Discount.</strong> Come, visit our Hotel at a special discounted rate.', );

$text[1] = array( "title"         =>    'Offers 2', 
"text"        =>    '25% Discount. Come, visit our Hotel at a special discounted rate.',   );

$text['_multiwidget'] = '1';

update_option( 'widget_text', $text );
$sidebars_widgets[ "primary-sidebar" ] = array("text-{$text[0]}","text-{$text[1]}");
update_option( 'sidebars_widgets', $sidebars_widgets );

Related posts

1 comment

  1. You’re quite off with your code. That’s not how widgets are stored (at least not since 2.8), plus you’re trying to inject arrays into strings!

    Setting default widgets (at least in the admin) seems a complex & messy task. I would much rather opt for default widgets in the display logic instead.

    <?php if ( ! dynamic_sidebar( 'primary-sidebar' ) ) : ?>
    
        <div class="widget">
            My default widgets!
        </div>
    
    <?php endif ?>
    

    The content within the if statement will print if no widgets have been configured, which will be the case on initial theme activation.

Comments are closed.