How can I hide certain sidebars from some users?

I’m working in wordpress multi site, but this question is relevant for regular use in wordpress.

How can I hide certain sidebars from certain user groups (e.g. editors or admins) ? I have already tried the trivial:

Read More
    if(is_admin() && user_can(...)){
       register_sidebar...
    }

But this doesn’t hide the sidebar, it shows that this sidebar is not active and the editor, for that matter, can still drag in and out as he or she pleases.

Thanks.

Related posts

Leave a Reply

1 comment

  1. I am assuming you mean to limit access to edit a sidebar in the admin interface, not to remove it on the theme.

    Without getting to deep into how the widgets/sidebar framework functions behind the scenes, your best bet is going to be to fiddle with the $wp_registered_sidebars global. However, the key is when – too early and the wp-admin/widgets.php template will see the missing sidebar and move it over to the Inactive Sidebars ‘sidebar’. Too late and, well, you get no effect.

    add_action('widgets_admin_page', 'sidebar_capabilities');
    
    /**
     * Keep in mind that you can certainly create custom
     * capabilities for your sidebars. You could create a loop
     * that generates new capabilities for each sidebar and assigns them
     * to admin. You could then manage those capabilities for other 
     * users with the Members plugin by Justin Tadlock
     */
    function sidebar_capabilities(){
        global $wp_registered_sidebars;
    
        //Remove the comment lines to see the global variable structure.
        //print_r($wp_registered_sidebars); 
    
        //Use whatever capabilities you want. 
        //To test as admin, just put junk text for the cap.
        if(is_admin() && !current_user_can('edit_plugins')){
    
            //This sidebar name is from the twenty-ten theme.
            unset($wp_registered_sidebars['primary-widget-area']);
        }
    }