[Resolved] Auto Remove Admin 'Sidebar Layout' and 'Footer Widget' for Contributors

Home Forums Support [Resolved] Auto Remove Admin 'Sidebar Layout' and 'Footer Widget' for Contributors

Home Forums Support Auto Remove Admin 'Sidebar Layout' and 'Footer Widget' for Contributors

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #39762
    Manny

    Hello All,

    Does anyone know how to remove the sidebar layout meta box OR the footer widget meta box from the backend view for those who aren’t admins?

    I’ve been trying the following with no luck. A look inside the inc/metaboxes file didn’t help as I guess I cannot pinpoint the id. The code below should work. Thank you!

    if( !current_user_can(‘administrator’) ) {
    // Function: Removes Sidebar Layout and Footer Widget Meta Boxes
    function remove_generate_metaboxes() {
    remove_meta_box( ‘generate_add_layout_meta_box’, ‘post’, ‘high’);
    remove_meta_box( ‘generate_layout_meta_box’, ‘post’, ‘high’);
    remove_meta_box( ‘generate_show_layout_meta_box’, ‘post’, ‘high’ );
    }
    add_action(‘add_meta_boxes_page’, ‘remove_generate_metaboxes’);
    } //end function
    } //end if not admin

    #39763
    Manny

    CORRECTION:

    add_action(‘add_meta_boxes’, ‘remove_generate_metaboxes’);

    INSTEAD OF:

    add_action(‘add_meta_boxes_page’, ‘remove_generate_metaboxes’);

    #39787
    Tom
    Lead Developer
    Lead Developer

    Hi Manny,

    By default, only administrators should be able to see these metaboxes.

    You can try that same function, but with this instead:

    add_action('add_meta_boxes', 'remove_generate_metaboxes', 9999);

    #39796
    Manny

    Thanks for the prompt reply Tom.

    Admins only yes, but I don’t want it to show for anyone below the ‘Editor’ role which, currently, it does. I log in with a contributors account and see the layout and footer widget metaboxes. Tried the recommended solution and the metaboxes remained. Will continue tinkering with this code. Again, much appreciate the support.

    #39800
    Tom
    Lead Developer
    Lead Developer

    Interesting – I’ll have to look into that, as I agree only administrators should be able to change those options.

    What might be happening is you have “post” set in your remove_metabox function:

    remove_meta_box( ‘generate_add_layout_meta_box’, ‘post‘, ‘high’);

    That will only remove it on the “post” post type, but not on “Pages” or any other post type.

    Just a guess?

    #39811
    Manny

    Again, thank you Tom.

    That is where I am specifically attempting to hide it from (“posts”) but I have also tried “pages” to no avail.

    Just tried it on another site to confirm and sidebar/footer widget metaboxes appears there as well for contributors. Contributors should be able to post but not alter the layout or footer widgets (at least that is how I am attempting to set it up).

    Tried seeing if a contributor can actually alter the sidebar/footer and indeed they can.

    Will continue to tinker on the code above to create a sort of patch. I could probably just edit the inc/metaboxes.php file with is_admin(); but upon an update that would go bye-bye.

    Much appreciate the prompt and courteous support.

    #39812
    Tom
    Lead Developer
    Lead Developer

    This may work:

    if (is_admin()) :
    function my_remove_meta_boxes() {
       if(!current_user_can('administrator')) {
          //remove_meta_box stuff
       }
    }
    add_action( 'admin_menu', 'my_remove_meta_boxes' );
    endif;
    
    #39816
    Manny

    Tom,

    I added to the inc/metaboxes.php file the following on the first line:

    if( current_user_can('administrator') ) {

    Then closed at bottom with:

    }

    And this has removed both metaboxes from all but admins view. I just have to remember upon an update to redo this 🙂

    Interestingly enough, it wouldn’t work if only applied to the function generate_add_layout_meta_box(). All related functions had to be encased to get it to work.

    Posted this ‘horrible fix’ in case another needs it in a pinch. I do not recommend this at all.

    #39817
    Manny

    Went back because your solution would be better (updating the functions.php file within child theme). However, after multiple attempted variations I was unable to achieve the desired effect. I even tried just removing the metaboxes for all users .

    
    function my_remove_meta_boxes() {
    remove_meta_box( 'generate_layout_meta_box', 'post', 99999);
    remove_meta_box( 'generate_save_layout_meta', 'post', 99999);
    }
    add_action( 'add_meta_boxes', 'my_remove_meta_boxes', 99999 );
    add_action( 'admin_menu', 'my_remove_meta_boxes', 99999 );
    add_action('save_post', 'my_remove_meta_boxes', 99999);
    
    #40497
    Tom
    Lead Developer
    Lead Developer

    I’ve made it so the next version will only show these metaboxes for admins.

    For those of you who want to change that, I added a filter so you can change the required capability.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.