0

I'm tired of editing functions.php every time I want to add/remove a sidebar.

I know there is Sidebar Generator plugin, but I wanted to develop something on my own that will work out of the box without plugins.

Anyways, I already know how to list all sidebars, how to register them and remove, but I'm not sure how to do this automatically from options page (I have one already of course).

I've analyzed the plugin's code but my PHP sucks so I don't understand half of it or even more.

Maybe someone here developed own sidebar generator and can help me with basics like how to register new sidebar using $_POST etc.?

I was thinking about fopen("functions.php", "r+"), but I'm not sure about this method, because it could even wipe out the whole functions.php file if used wrong, searching for already registered widgets could be painful too.

I believe it'd be very useful to have something like that, and that's strange there's no in built sidebar generator in WP.

1 Answer 1

4

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.

7
  • @Adam Thompson, thank you, that's always something. Can you explain me how and where exactly these sidebars are being added or removed from? I thought every sidebar should be registered in functions.php but here it seems like they're being registered somewhere else? I believe I'm talking about 'sidebar_generator::update_sidebars($sidebars);'. Commented Mar 17, 2011 at 20:07
  • @Adam Thompson I've been searching for sidebars anywhere in WP MySQL database but no luck, the only place where I found them 'sbg_sidebars' in wp_options table as you said, but it seems like this plugin itself creates this row, so how WP does know sidebars are stored there? Commented Mar 17, 2011 at 20:16
  • @Wordpressor: sidebars are not stored in the database. They are "stored" in PHP. That's how WP knows about them.
    – scribu
    Commented Mar 17, 2011 at 20:52
  • @scribu, what "stored in PHP" does mean? Variables, arrays, anything, they have to be stored somehow, in files, or databases? And in this case there's nothing in functions.php, so user created sidebars must be, and are, stored in 'sbg_sidebars'. The point is how WP knows about the whole 'sbg_sidebars'. Commented Mar 17, 2011 at 21:44
  • Read the first paragraph again: "The init function loops through all the sidebars (stored in the wp_options table) and registers them in wordpress". That's how WP knows.
    – scribu
    Commented Mar 18, 2011 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.