Sidebar Generator? :)

The code for this plugin is actually pretty simple. The init function loops through all the sidebars (stored in the wp_options table) and registers them in wordpress:

<?php
function init(){
        //go through each sidebar and register it
        $sidebars = sidebar_generator::get_sidebars();


        if(is_array($sidebars)){
            foreach($sidebars as $sidebar){
                $sidebar_class = sidebar_generator::name_to_class($sidebar);
                register_sidebar(array(
                    'name'=>$sidebar,
                    'before_widget' => '<li id="%1$s" class="widget sbg_widget '.$sidebar_class.' %2$s">',
                    'after_widget' => '</li>',
                    'before_title' => '<h2 class="widgettitle sbg_title">',
                    'after_title' => '</h2>',
                ));
            }
        }
    }
?>

The sidebars are added/removed with the add_sidebar() and remove_sidebar() methods respectively. The $_POST[‘sidebar_name’] gets stored in the $name variable as the sidebars name:

<?php
function add_sidebar(){
        $sidebars = sidebar_generator::get_sidebars();
        $name = str_replace(array("\n","\r","\t"),'',$_POST['sidebar_name']);
        $id = sidebar_generator::name_to_class($name);
        if(isset($sidebars[$id])){
            die("alert('Sidebar already exists, please use a different name.')");
        }

        $sidebars[$id] = $name;
        sidebar_generator::update_sidebars($sidebars);

        $js = "
            var tbl = document.getElementById('sbg_table');
            var lastRow = tbl.rows.length;
            // if there's no header row in the table, then iteration = lastRow + 1
            var iteration = lastRow;
            var row = tbl.insertRow(lastRow);

            // left cell
            var cellLeft = row.insertCell(0);
            var textNode = document.createTextNode('$name');
            cellLeft.appendChild(textNode);

            //middle cell
            var cellLeft = row.insertCell(1);
            var textNode = document.createTextNode('$id');
            cellLeft.appendChild(textNode);

            //var cellLeft = row.insertCell(2);
            //var textNode = document.createTextNode('[<a href=\'javascript:void(0);\' onclick=\'return remove_sidebar_link($name);\'>Remove</a>]');
            //cellLeft.appendChild(textNode)

            var cellLeft = row.insertCell(2);
            removeLink = document.createElement('a');
            linkText = document.createTextNode('remove');
            removeLink.setAttribute('onclick', 'remove_sidebar_link(\'$name\')');
            removeLink.setAttribute('href', 'javacript:void(0)');

            removeLink.appendChild(linkText);
            cellLeft.appendChild(removeLink);


        ";


        die( "$js");
    }

    function remove_sidebar(){
        $sidebars = sidebar_generator::get_sidebars();
        $name = str_replace(array("\n","\r","\t"),'',$_POST['sidebar_name']);
        $id = sidebar_generator::name_to_class($name);
        if(!isset($sidebars[$id])){
            die("alert('Sidebar does not exist.')");
        }
        $row_number = $_POST['row_number'];
        unset($sidebars[$id]);
        sidebar_generator::update_sidebars($sidebars);
        $js = "
            var tbl = document.getElementById('sbg_table');
            tbl.deleteRow($row_number)

        ";
        die($js);
    }
?>

The sidebars are loaded and registered into the theme with the get_sidebar method

<?php
function get_sidebar($name="0"){
        if(!is_singular()){
            if($name != "0"){
                dynamic_sidebar($name);
            }else{
                dynamic_sidebar();
            }
            return;//dont do anything
        }
        global $wp_query;
        $post = $wp_query->get_queried_object();
        $selected_sidebar = get_post_meta($post->ID, 'sbg_selected_sidebar', true);
        $selected_sidebar_replacement = get_post_meta($post->ID, 'sbg_selected_sidebar_replacement', true);
        $did_sidebar = false;
        //this page uses a generated sidebar
        if($selected_sidebar != '' && $selected_sidebar != "0"){
            echo "\n\n<!-- begin generated sidebar -->\n";
            if(is_array($selected_sidebar) && !empty($selected_sidebar)){
                for($i=0;$i<sizeof($selected_sidebar);$i++){                    

                    if($name == "0" && $selected_sidebar[$i] == "0" &&  $selected_sidebar_replacement[$i] == "0"){
                        //echo "\n\n<!-- [called $name selected {$selected_sidebar[$i]} replacement {$selected_sidebar_replacement[$i]}] -->";
                        dynamic_sidebar();//default behavior
                        $did_sidebar = true;
                        break;
                    }elseif($name == "0" && $selected_sidebar[$i] == "0"){
                        //we are replacing the default sidebar with something
                        //echo "\n\n<!-- [called $name selected {$selected_sidebar[$i]} replacement {$selected_sidebar_replacement[$i]}] -->";
                        dynamic_sidebar($selected_sidebar_replacement[$i]);//default behavior
                        $did_sidebar = true;
                        break;
                    }elseif($selected_sidebar[$i] == $name){
                        //we are replacing this $name
                        //echo "\n\n<!-- [called $name selected {$selected_sidebar[$i]} replacement {$selected_sidebar_replacement[$i]}] -->";
                        $did_sidebar = true;
                        dynamic_sidebar($selected_sidebar_replacement[$i]);//default behavior
                        break;
                    }
                    //echo "<!-- called=$name selected={$selected_sidebar[$i]} replacement={$selected_sidebar_replacement[$i]} -->\n";
                }
            }
            if($did_sidebar == true){
                echo "\n<!-- end generated sidebar -->\n\n";
                return;
            }
            //go through without finding any replacements, lets just send them what they asked for
            if($name != "0"){
                dynamic_sidebar($name);
            }else{
                dynamic_sidebar();
            }
            echo "\n<!-- end generated sidebar -->\n\n";
            return;         
        }else{
            if($name != "0"){
                dynamic_sidebar($name);
            }else{
                dynamic_sidebar();
            }
        }
    }
?>

We actually load the sidebar array from the wp_options table with the following:

<?php
function get_sidebars(){
        $sidebars = get_option('sbg_sidebars');
        return $sidebars;
    }
?>

This code is really pretty simple once you know PHP, so may I suggest taking a little time to become more familiar with it? It really is a great skill to have. Hopefully this helps point you in the right direction.

Leave a Comment

tech