0

My question is pretty similar to this one:

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

Except that it's for Twenty Fourteen. I'm trying to use it as a base for my own theme, and I don't see where in the code it's populating the default widgets in the main sidebar: Search Box, recent entries, recent comments, etc.

In my linked question, the answer turned out to be that they were hardcoded in sidebar.php, but in Twenty Fourteen, as far as I can tell, it's not hardcoded there, nor it's defined in functions.php, when the sidebar is initialized (in twentyfourteen_widgets_init()). So how did they do it?

  • Do you mean that widgets are being assigned to the sidebar area on theme activation in 'Appearance > Widgets'? Or do you mean that there is default content in the sidebar even when no widgets are assigned? – Jami Gibbs Jul 2 '15 at 19:20
  • I mean the widgets assigned on the sidebar by default: search box, recent entries, recent comments... – PaulJ Jul 2 '15 at 19:24
  • 1
    Read this specific answer: wordpress.stackexchange.com/a/51086/14225 – marcovega Jul 2 '15 at 19:47
  • @marcovega: thanks a lot for that link, it was very useful and complemented RachieVee's answer below. My question, though, is: where do you come up with the numbers that are appended to the widgets? I.e.: the default widgets are initialized as "search-2", "recent-posts-2", while your linked answer takes those numbers from a $counter. I understand it's to avoid collisions, but... collisions between what? Instances of the same widget in the same sidebar? Instances of the same widget in different sidebars? Basically, I'm confused about where should I start the $counter in my own code... – PaulJ Jul 3 '15 at 17:04
2

In Twenty Fourteen, when you first activate the theme without any changes made to the theme, the widgets that appear there on the sidebar are from the dashboard. You can remove them from Appearance > Widgets and you'll see Search, Recent Posts, Recent Comments etc. in the meta-box "Primary Sidebar".

If you want to see the code for the primary sidebar, it's in the sidebar.php file and looks like this:

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>

Also if you're just curious how WordPress sets up default widgets (in case I misunderstood your question), WordPress does this in a core file in wp-admin/includes/upgrade.php.

Here you'll find the code that sets up the default widgets on a fresh install of WordPress with a theme. It should look like this on line 234ish:

// Set up default widgets for default theme.
update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
update_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
update_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
update_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
update_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
update_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
update_option( 'sidebars_widgets', array ( 'wp_inactive_widgets' => array (), 'sidebar-1' => array ( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2', ), 'array_version' => 3 ) );

Hope that helps. :-)

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.