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 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 );
share|improve this question
    
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

1 Answer 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.

share|improve this answer
    
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

 
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.