Assigning rights to change sidebars

The Custom Sidebars Pro plugin is great. I'm struggling with a use case that perhaps isn't as you intended, but I'll throw it out there and see what you think.

My site is primarily based on pages. I have several user types that allows for the editing of pages, but not creation. I also have the typical subscriber role for posting comments, and such.

The problem I am facing is that I don't seem to be able to control WHO is able to change/select a new sidebar to appear in pages/posts. It appears that all users have the right to assign a new sidebar, regardless of their WordPress rights.

Am I missing something?

  • Tyler Postle

    Hey Timmo,

    Sorry for my delay here.

    This might not be the most efficient way to resolve this, but it should work and also gives you some flexibility :slight_smile:

    Add the following code to your themes' functions.php or as an mu-plugin:

    // Add role class to body
    function add_role_to_body($classes) {
    
    	global $current_user;
    	$user_role = array_shift($current_user->roles);
    
    	$classes .= 'role-'. $user_role;
    	return $classes;
    }
    add_filter('body_class','add_role_to_body');
    add_filter('admin_body_class', 'add_role_to_body');

    That will add a new user role based CSS class to the "body" tag of each page.

    So now you can hide the custom sidebar meta box by adding the following CSS:

    .role-subscriber #customsidebars-mb {
    display: none;
    }

    Replace "subscriber" with the user role you would like to hide it for, or you can hide it for multiple roles like this:

    .role-subscriber #customsidebars-mb, .role-editor #customsidebars-mb {
    display: none;
    }

    If you don't already have a custom CSS area then you can use this plugin here: https://en-ca.wordpress.org/plugins/simple-custom-css/

    If you would rather go the mu-plugins route instead of adding the code to your functions.php (ie. you plan on swapping themes) then I've attached the zipped up plugin file, just unzip and upload the file to /wp-content/mu-plugins/ if mu-plugins folder doesnt' exist then just create it.

    Hope that helps!

    All the best,
    Tyler

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.