Initializing the sidebars

This method will reduce the amount of cut and pasting you do when registering sidebars. It basically allows you to have a central location for each of the $vars that are consistent throughout all of the sidebars and just loops through them creating each of the sidebars based on the title.

if (!function_exists('mytheme_sidebar_init')) {
	function mytheme_sidebar_init() {
		$before_widget = "<div id=\"".'%1$s'."\" class=\"widget ".'%2$s'."\">\r\n<div class=\"widget-content\">\r\n";
		$after_widget = "</div>\r\n</div>\r\n";
		foreach (array(
			__('Above Header', 'mytheme'),
			__('Header', 'mytheme'),
			__('Menubar', 'mytheme'),
			__('Left Sidebar', 'mytheme'),
			__('Right Sidebar', 'mytheme'),
			__('Over Blog', 'mytheme'),
			__('Under Blog', 'mytheme'),
			__('The Footer', 'mytheme') 
		) as $sidebartitle) {
			'name'=> $sidebartitle,
			'id' => sanitize_title($sidebartitle),
//			'description' => $sidebartitle, 
			'before_widget' => $before_widget,
			'after_widget'  => $after_widget,
			'before_title'  => "<h2 class=\"widgettitle\">",
			'after_title'   => "</h2>\r\n"

Getting the sidebars

Now that we have the sidebar’s registered and ready to be used in your theme, with this next chunk of code, you do *not* need to have all of those individual sidebar-$location.php files in your theme and it creates a dynamic one for them all. However it also checks if there *is* a sidebar-$location.php file if it does exist it will use it.

function mytheme_get_sidebar($location = '') {
	if (empty($location)) { get_sidebar(); return; }
	if (file_exists(get_stylesheet_directory().'/sidebar-'.$location.'.php')) {
	} elseif (is_active_sidebar('sidebar-'.$location)) { ?>
		<div id="sidebar-<?php echo $location; ?>" class="sidebar">
			<?php dynamic_sidebar('sidebar-'.$location); ?>
	<?php }

P.S. You probably can take out the elseif (is_active_sidebar( statement and have it just go else { – before the creation of the dynamic sidebar if you use the if (is_active_sidebar() code before the call to the mytheme_get_sidebar() function.

First thing this function does if checks if you have a $location, if it doesn’t it will just load up the generic sidebar, which comes from the sidebar.php file from the theme. Next, it checks if the sidebar-$location.php exists and uses it if it does. After that it just creates the dynamic sidebar location with divs.

Using the mytheme_get_sidebar($location);

In this example, it will check if the sidebar is ‘active’ meaning if there’s actually any widgets in it, if there are it will then try to render the sidebar. If there is a file in the theme/child theme’s directory called sidebar-over.php it will use that, if not it will use the generic div’s created by the function. If it’s a generated sidebar, the CSS for it will be #sidebar-over {} with the class of .sidebar {} in this example.

<?php if (is_active_sidebar('over-blog')) mytheme_get_sidebar('over'); ?>

Else, if you want it to stay rendered in the theme for CSS sake, to keep the block there just do the same without the is_active_sidebar() code.

<?php mytheme_get_sidebar('over'); ?>

And there you have it.

– Phil