Programmatically add widgets to sidebars
Is it possible to develop a theme that has certain widgets automatically activated when the theme is activated?
I’ve got a sidebar defined in my functions.php:
if ( function_exists('register_sidebar') ) register_sidebar(array( 'name' => 'Default Sidebar', 'id' => 'default-sidebar', 'before_widget' => '<div class="widget">', 'after_widget' => '</div>', 'before_title' => '<h3>', 'after_title' => '</h3>' ));
Now in the WP backend there would be an empty sidebar called “Default Sidebar” in the Widgets section under Appearance. Instead of an empty sidebar, I’d like to have a sidebar filled with pre-activated widgets that I defined e.g. in the functions.php with something like that:
activated_by_default( array( 'WP_Widget_Tag_Cloud', 'WP_Widget_Archives') );
I know there’s a solution by “hardcoding” the widgets in the theme:
<ul id="sidebar"> <?php if ( ! dynamic_sidebar() ) : ?> <li><?php wp_tag_cloud( $args ); ?></li> <li><?php wp_get_archives( $args ); ?></li> <?php endif; ?> </ul>
But that doesn’t fit to my esp. not to the user’s needs because the user a) gets an empty sidebar in the backend although there are widgets displayed in the frontend which maybe confuses him and b) when he adds a widget to the sidebar through the backend the pre-defined will disappear which maybe confuses him a bit more.
I could delete the
<?php if ( ! dynamic_sidebar() ) : ?>
and put the dynamic sidebar below the widgets I want to be displayed by default, so that the pre-defined widgets won’t disappear when the user adds one through the backend. But I want the user to be able to order ALL widgets which wouldn’t be possible when some are hardcoded.