Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

    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.

share|improve this question
1  
hide where? on the admin side or front end? –  Bainternet Jun 26 '12 at 21:36
    
In the admin. I want to allow admins control over 2 widget areas, when actually superadmins can control 4 widget areas. The users seee them all. –  Nadav Jun 28 '12 at 15:50

1 Answer 1

up vote 0 down vote accepted

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']);
    }
}
share|improve this answer
    
Thanks, It seems to be working! –  Nadav Jun 28 '12 at 11:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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