Dynamically create and attach sidebars to pages or posts
wordpress snippet
This is a bit large for the snippets I normally post however very useful. This snippet will let you easily create new sidebars by simply adding the name to an array. You will also be able to define the dynamically created sidebar a page loads by selecting from the custom sidebar selection metabox.
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 -->
this months featured snippets
- Hakan
- http://wpsnipp.com Kevin Chard
- http://wpsnipp.com Kevin Chard
- http://twitter.com/AntJanus Antonin Januska
- http://wpsnipp.com Kevin Chard
- http://about.me/antoninjanuska Antonin Januska
- http://twitter.com/AntJanus Antonin Januska
- http://wpsnipp.com Kevin Chard
- Hakan
- Hakan
- http://wpsnipp.com Kevin Chard
- http://tightmixblog.com Chris B.
- http://wpsnipp.com Kevin Chard
- http://tightmixblog.com Chris B.
- http://wpsnipp.com Kevin Chard
- Ajayvats
- http://wpsnipp.com Kevin Chard
- http://www.dietmarhartje.de/ Info
- http://wpsnipp.com Kevin Chard
- http://twitter.com/SimonWebDesign Simon Urbina
- http://www.facebook.com/drbeaven Daniel Beaven
- http://wpsnipp.com Kevin Chard
- Anonymous
- Himanshu Jain
- Surb
- j.r.





