How can I insert default widgets when my theme is activated (similar to what twenty eleven does)?

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

Related posts

Leave a Reply

3 comments

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

  2. 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>';
    }