Turn off "Screen Options" meta boxes for a particular role

Hello!

I read in another thread that I can use the following function to turn off Screen Options, and it did test properly:

// REMOVE POST META BOXES
function remove_my_post_metaboxes() {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Status Metabox
remove_meta_box( 'commentsdiv','post','normal' ); // Comments Metabox
remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
remove_meta_box( 'revisionsdiv','post','normal' ); // Revisions Metabox
remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
remove_meta_box( 'trackbacksdiv','post','normal' ); // Trackback Metabox
remove_meta_box( 'categorydiv','post','normal' ); // Categories Metabox
remove_meta_box( 'formatdiv','post','normal' ); // Formats Metabox
remove_meta_box( 'postimagediv','post','normal' ); // Featured Image Metabox
remove_meta_box( 'submitdiv','post','normal' ); // Categories Metabox
remove_meta_box( 'tagsdiv-post_tag','post','normal' ); // Tags Metabox
}
add_action('admin_menu','remove_my_post_metaboxes');

But I have 2 questions regarding this:

1. Can I make this function apply explicitly to the Contributor role? Can something be added to the function to accomplish this?

2. There are additional Screen Options on our site due to plugin features. How would I determine those Screen Options' names (such as the "categorydiv" that represents the Categories Metabox)? My attached image is to illustrate this; but I realize you cannot determine their names from the screen shot. Is there a place that I would look to determine the code names for these meta boxes?

Thank you for your help!

James

  • Michael Bissett

    Hey @James Wolfensberger, hope you're doing well! :slight_smile:

    Regarding your first question, you could use an if statement like this one to handle that:

    // REMOVE POST META BOXES
    function remove_my_post_metaboxes() {
    	if ( !current_user_can('upload_files') ) {
    
    		remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
    		remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Status Metabox
    		remove_meta_box( 'commentsdiv','post','normal' ); // Comments Metabox
    		remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
    		remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
    		remove_meta_box( 'revisionsdiv','post','normal' ); // Revisions Metabox
    		remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
    		remove_meta_box( 'trackbacksdiv','post','normal' ); // Trackback Metabox
    		remove_meta_box( 'categorydiv','post','normal' ); // Categories Metabox
    		remove_meta_box( 'formatdiv','post','normal' ); // Formats Metabox
    		remove_meta_box( 'postimagediv','post','normal' ); // Featured Image Metabox
    		remove_meta_box( 'submitdiv','post','normal' ); // Categories Metabox
    		remove_meta_box( 'tagsdiv-post_tag','post','normal' ); // Tags Metabox
    
    	}
    }
    add_action('admin_menu','remove_my_post_metaboxes');

    Regarding your 2nd question, I would want to look for any mention of the "add_meta_boxes" function in the code, since that's what the plugins would be hooking into to add their meta boxes:

    http://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes

    You should be able to find the names from there. :slight_smile:

    Hope this helps!

    Kind Regards,
    Michael

  • James Wolfensberger

    Thanks Michael - but as soon as I include the if statement in the provided code, the meta boxes are no longer hidden. I've been careful to copy/paste your code precisely. If I revert back to the original function with no if statement, it works again but is not limited to the contributor role.

    I'm guessing that if this can be corrected, that it will not impact Admin accounts, even with the current_user_can('upload_files') reference?

    I appreciate your help!

    James

  • Bojan Radonic

    Hey there @James Wolfensberger, hope you're well today!

    Please try replacing the code with this one:

    // REMOVE POST META BOXES
    function remove_my_post_metaboxes() {
    	if ( current_user_can('contributor') ) {
    
    		remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
    		remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Status Metabox
    		remove_meta_box( 'commentsdiv','post','normal' ); // Comments Metabox
    		remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
    		remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
    		remove_meta_box( 'revisionsdiv','post','normal' ); // Revisions Metabox
    		remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
    		remove_meta_box( 'trackbacksdiv','post','normal' ); // Trackback Metabox
    		remove_meta_box( 'categorydiv','post','normal' ); // Categories Metabox
    		remove_meta_box( 'formatdiv','post','normal' ); // Formats Metabox
    		remove_meta_box( 'postimagediv','post','normal' ); // Featured Image Metabox
    		remove_meta_box( 'submitdiv','post','normal' ); // Categories Metabox
    		remove_meta_box( 'tagsdiv-post_tag','post','normal' ); // Tags Metabox
    
    	}
    }
    add_action('admin_menu','remove_my_post_metaboxes');
    ?>

    This should remove the meta boxes only for contributors.

    Please let me know if this works :slight_smile:

    Best regards,
    Bojan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.