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.

When Twenty Eleven theme is activated, it inserts several default widgets into its sidebars. How can I code my theme to do the same?

share|improve this question
    
You really should consider the most upvoted answer ;) –  Anto Jun 19 '13 at 17:41

3 Answers 3

up vote 1 down vote accepted

Johannes Pille is right TwentyEleven's widgets such as Archives and Meta are hardcoded into their sidebar.php

Here is what I would do:

First define your sidebars in your functions.php like so:

 if ( function_exists('register_sidebars') )
    register_sidebar(array(
    'name'=>'Left Sidebar',
    'before_title' => '<h2 class="label">',
        'after_title' => '</h2>',
    'description' => 'Items placed here will be shown in the left sidebar.'));

As you can see in the above code you are naming the sidebar "Left Sidebar". You can change this to whatever you want but I generally choose to use placement terms so that way I know what part of the page the widget will show on.

Next you will want to put this in your theme where you want the sidebar to show:

<?php if(function_exists('dynamic_sidebar') && dynamic_sidebar('Left Sidebar')):else: ?>

Now you will want to put this next:

<li id="calendar">
        <h2>
          <?php _e('Calendar'); ?>
        </h2>
        <?php get_calendar(); ?>
      </li>
      <?php wp_list_pages('title_li=<h2>Pages</h2>'); ?>
      <li>
        <h2>
          <?php _e('Categories'); ?>
        </h2>
        <ul>
          <?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?>
        </ul>
      </li>
      <li>
        <h2>
          <?php _e('Archives'); ?>
        </h2>
        <ul>
          <?php wp_get_archives('type=monthly'); ?>
        </ul>
      </li>
      <?php get_links_list(); ?>
      <li>
        <h2>
          <?php _e('Meta'); ?>
        </h2>
        <ul>
          <?php wp_register(); ?>
          <li>
            <?php wp_loginout(); ?>
          </li>
          <?php wp_meta(); ?>
        </ul>
      </li>
      <?php endif; ?>
    </ul>

The

closes the "if" function at the beginning of this string. What this will do is if there are no widegts place in the sidebar these default ones will appear until one is added.

If you want them there all the time without people changing it but still want them to be able to add content of their own you can just place the hardcoded widgets outside the "if" statements like this:

<li id="calendar">
        <h2>
          <?php _e('Calendar'); ?>
        </h2>
        <?php get_calendar(); ?>
      </li>
      <?php wp_list_pages('title_li=<h2>Pages</h2>'); ?>
      <li>
        <h2>
          <?php _e('Categories'); ?>
        </h2>
        <ul>
          <?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?>
        </ul>
      </li>
      <li>
        <h2>
          <?php _e('Archives'); ?>
        </h2>
        <ul>
          <?php wp_get_archives('type=monthly'); ?>
        </ul>
      </li>
      <?php get_links_list(); ?>
      <li>
        <h2>
          <?php _e('Meta'); ?>
        </h2>
        <ul>
          <?php wp_register(); ?>
          <li>
            <?php wp_loginout(); ?>
          </li>
          <?php wp_meta(); ?>
        </ul>
      </li>

    </ul>
<?php if(function_exists('dynamic_sidebar') && dynamic_sidebar('Left Sidebar')):else: ?>
<?php endif; ?>

I will suggest in order to be adding these to your theme (in the case you want something else) that you look at the WordPress Codex Function reference here: http://codex.wordpress.org/Function_Reference#Theme-Related_Functions

.....................................................................................................................................................................

Here is how to have a set of default widgets for in the admin area.

First you must know that WordPress comes with 12 standard widgets. You can view them here: http://codex.wordpress.org/Function_Reference/the_widget

If you want to remove some of the standard widgets here is how you go about doing so:

function remove_some_wp_widgets(){
unregister_widget('WP_Widget_Calendar'); //removes calendar widget
unregister_widget('WP_Widget_Search'); // removes the search widget
unregister_widget('WP_Widget_Recent_Comments'); // removes recent comments widget 
}

add_action('widgets_init',remove_some_wp_widgets', 1);

You would add the above code into your functions.php file of your theme. This will hide them. (not necessarily remove them)

If you want to add a custom widget to the admin area i would suggest that you read this article on how to register a new widget.

http://dev7studios.com/resources/wordpress-development-for-designers-part-1/

share|improve this answer
    
Hi Nicole, I follow you here in relation to the sidebar.php on the live site, however, how are you setting the sidebar widgets to appear in the widgets admin section? –  N2Mystic Dec 5 '11 at 20:35
    
Oh so your wondering about a default set of widgets on the admin side? –  Nicole Dec 5 '11 at 20:50
    
Please check out the revisions i made above. –  Nicole Dec 5 '11 at 21:09
    
thanks, but I micommunicated. I'm talking about the sidebar widgets that appear under "Main Sidebar" and "Footer Area Two" over on the right hand side of widget admin. Those sidebars are prepopulated with widgets as if they had been drag/dropped to the sidebars. I can do that on the live site, just don't know how to pre-populate the widget areas themselves. –  N2Mystic Dec 5 '11 at 21:24
    
Oh ok well you can do with what was mentioned before and just put it in a different area. –  Nicole Dec 5 '11 at 21:54

The (pseudo-)widgets titled "Archives" and "Meta" are hardcoded into TwentyEleven's sidebar.php.

share|improve this answer

In your themes sidebar pages you can can check to see if a widget is active in your sidebar, and then do an if, else.

//Check to see if your sidebar "main" is active.  Change to match your registered sidebar
if (is_active_sidebar('main')) {

    //echo holder div.  Give it a class for css
    echo '<div class="homeSidebar">';
    dynamic_sidebar('main');
    echo '</div>';
}

//if there is no active widget in this side bar, use this one.  
else {
    echo '<div class="homeSidebar">';

    //Run the_widget() to bring in the widget you want by default.  Take a look at http://codex.wordpress.org/Function_Reference/the_widget for more info
    the_widget('wp_widget_archives');
    echo '</div>';
}
share|improve this answer
    
The only valid answer - good to read +1. –  kaiser Aug 30 '12 at 13:07
    
+1 This should be the accepted answer –  Tom J Nowell Aug 30 '12 at 13:11

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.