Dynamically create and attach sidebars to pages or posts

Go to source Adding the first snippet to the functions.php of your wordpress theme will do a few things. First it will create a new metabox within the pages editor letting you attach one of the dynamically created sidebars. Second you will notice an array of names eg: Sidebar 01, Sidebar 02 etc. these are the names of the dynamically created sidebars that you can change or add new sidebars to. Copy the second snippet of code and replace all the code within your sidebar.php template of your wordpress theme and you are all set. ( example screenshot )

snippet :  PHP – functions.phpcopy
    $dynamic_widget_areas = array(
		/* rename or create new dynamic sidebars */
		"Sidebar 01",
		"Sidebar 02",
		"Sidebar 03",
		"Sidebar 04",
		"Sidebar 05",
		"Sidebar 06",
		"Sidebar 07",
		"Search Template",
		);
if ( function_exists('register_sidebar') ) {
    foreach ($dynamic_widget_areas as $widget_area_name) {
        register_sidebar(array(
           'name'=> $widget_area_name,
           'before_widget' => '<div id="%1$s" class="widget %2$s left half">',
           'after_widget' => '</div>',
           'before_title' => '<h3 class="widgettitle">',
           'after_title' => '</h3>',
        ));
    }
}
	add_action("admin_init", "sidebar_init");
	add_action('save_post', 'save_sidebar_link');
	function sidebar_init(){
		add_meta_box("sidebar_meta", "Sidebar Selection", "sidebar_link", "page", "side", "default");
	}
	function sidebar_link(){
		global $post, $dynamic_widget_areas;
		$custom  = get_post_custom($post->ID);
		$link    = $custom["_sidebar"][0];
	?>
<div class="link_header">
	<?
	echo '<select name="link" class="sidebar-selection">';
	echo '<option>Select Sidebar</option>';
	echo '<option>-----------------------</option>';
	foreach ( $dynamic_widget_areas as $list ){
		    if($link == $list){
		      echo '<option value="'.$list.'" selected="true">'.$list.'</option>';
			}else{
		      echo '<option value="'.$list.'">'.$list.'</option>';
			}
		}
	echo '</select><br />';
	?>
</div>
<p>Select sidebar to use on this page.</p>
<?php
}
function save_sidebar_link(){
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {return $post->ID;}
	update_post_meta($post->ID, "_sidebar", $_POST["link"]);
}
add_action('admin_head', 'sidebar_css');
function sidebar_css() {
	echo'
	<style type="text/css">
		.sidebar-selection{width:100%;}
	</style>
	';
}
snippet :  PHP – sidebar.phpcopy
<!-- begin sidebar -->
<div id="sidebar">
<?
	   global $post;
	   $custom  = get_post_custom($post->ID);
	   $link    = $custom["_sidebar"][0];
	if($link != ''){
	    echo '<ul id="widgets">';
		if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar($link) ) :
		endif;
	    echo '</ul>';
	}
?>
</div>
<!-- end sidebar -->


Dynamically create and attach sidebars to pages or posts is a post from: wpsnipp.comWordPress Snippets, Code, Hacks, 590+ for your theme, blog
which is not allowed to be copied on other sites.”

Share this post.


Dynamically create and attach sidebars to pages or posts“>Facebook | Dynamically create and attach sidebars to pages or posts“>StumbleUpon | Dynamically create and attach sidebars to pages or posts“>Delicious | Dynamically create and attach sidebars to pages or posts @wpsnipp”>Tweet It | Dynamically create and attach sidebars to pages or posts“>Digg This

© wpsnipp.com All Rights reserved.

source: wpsnipp.com