Skip to:
Content
Pages
Categories
Search
Top
Bottom

How do I unregister a sidebar?

  • @takeo

    Participant

    I’m creating a child theme based on the default child theme. I’ve decided to use a simple 2 column layout on the homepage. I found out how to change the layout easily enough simply by linking to a different layout css file. Very nice. However, since I won’t be using the ‘third-section’ sidebar I would like to also remove it from the admin area so as to not confuse admin users. I tried adding a functions.php file to my child theme with this code but it does not work. Help :(

    <?php
    function remove_sidebar() {
    unregister_sidebar('third-section');
    }
    add_action( 'admin_init', 'remove_sidebar');
    ?>

    It would be nice to rename the existing ones as well. I want to show non-members (not logged in) the blog sidebar on the homepage… and then swap that out with a members sidebar when people log in. The logic for that in the home.php template is dead simple… but it would be more intuitive if I could rename ‘second-section’ to ‘members-sidebar’. I could just change the parent theme of course… but that just wouldn’t be right.

    p.s. WPMU 2.8.4a + BP1.1 and no plugins.

Viewing 4 replies - 1 through 4 (of 4 total)
  • In your bp-sn-parent theme folder look in functions.php and delete the code in there. Then, in home.php delete the appropriate code. You can re-name them in functions.php as well. Here is one I’ve added as an example (it’s also the code you need to delete from functions.php):

    register_sidebars( 1,

    array(

    'name' => 'groups-page',

    'before_widget' => '<div id="%1$s" class="widget %2$s">',

    'after_widget' => '</div>',

    'before_title' => '<h2 class="widgettitleevents">',

    'after_title' => '</h2>'

    )

    );

    This is the code you need to remove from home.php:

    <div id="third-section" class="widget-section">

    <?php if ( !function_exists('dynamic_sidebar')

    || !dynamic_sidebar('third-section') ) : ?>

    <div class="widget-error">

    <?php _e( 'Please log in and add widgets to this section.', 'buddypress' ) ?> /wp-admin/widgets.php?s=&show=&sidebar=first-section"><?php _e( 'Add Widgets', 'buddypress' ) ?>

    </div>

    <?php endif; ?>

    This should answer your question. ;-)

    @apeatling

    Keymaster

    Don’t touch anything in bp-sn-parent. You should override the code in your child theme only.

    @takeo

    Participant

    Exactly Andy. That’s my issue. Hacking the parent function.php file is easy. But I don’t want to do that. So I created a function.php file in my child theme but the unregister_sidebar() function doesn’t seem to work :( I suspect this is a WPMU thing… not a BuddyPress thing. Here is the code I added to function.php in my child theme. Should work. Doesn’t. I’m at bit of a loss. I tried adding different priority levels to add_action() and they had no effect.

    <?php
    function remove_sidebar() {
    unregister_sidebar('third-section');
    }
    add_action( 'admin_init', 'remove_sidebar');
    ?>

    p.s. https://codex.wordpress.org/Function_Reference/unregister_sidebar

    @takeo

    Participant

    Solved!!!

    unregister_sidebar() keys off the sidebar id. I assumed this to be the same argument as name. It is not. However, the BuddyPress parent theme does not set sidebar id’s. So how to unregister? Codex reveals that when no id is explicitly set, the id’s default to sidebar-1, sidebar-2, sidebar-3… etc. So the code to unregister the “third-section” sidebar (for instance) is as follows (in functions.php of your child theme).

    <?php
    function remove_sidebar() {
    unregister_sidebar('sidebar-3');
    }
    add_action( 'admin_init', 'remove_sidebar');
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How do I unregister a sidebar?’ is closed to new replies.
Skip to toolbar