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 );
  • Can you show your code of displaying default widgets? – Vinod Dalvi Jun 24 '13 at 4:36
  • yeah sure. will paste it here – Shaikh Aezaz Jun 24 '13 at 4:50
  • Question is edited – Shaikh Aezaz Jun 24 '13 at 5:05

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!

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

  • Ok. but this will call every time when primary-sidebar is empty and i want to do this only when theme is activate – Shaikh Aezaz Jun 24 '13 at 7:10
  • Nothing "calls". It's simply placeholder content in your sidebar.php. A lot of authors do this! – TheDeadMedic Jun 24 '13 at 7:21
  • Ok. so if i removed default widgets from sidebar then it will display default sidebar or it will not display? – Shaikh Aezaz Jun 24 '13 at 7:40
  • and how do i call my default widget inside div ? "My default widgets!". – Shaikh Aezaz Jun 24 '13 at 7:41
  • If there are no widgets configured in your primary sidebar, then any HTML, PHP etc. inside that if will be displayed instead. I can't see how I can make it any clearer! – TheDeadMedic Jun 24 '13 at 8:42

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.