1

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.

2
  • 1
    hide where? on the admin side or front end?
    – Bainternet
    Commented Jun 26, 2012 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
    Commented Jun 28, 2012 at 15:50

1 Answer 1

0

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']);
    }
}
0

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.