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.

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

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.

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

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.

<br />
&lt;?php if (is_active_sidebar('over-blog')) mytheme_get_sidebar('over'); ?&gt;<br />

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.

<br />
&lt;?php mytheme_get_sidebar('over'); ?&gt;<br />

And there you have it.

– Phil