Hide Editor Panels From Non-Administrators
Removes rarely needed panels like Discussion, Custom Fields and the slug box from the editor for everyone but administrators, in both editors.
Opens WPIDE on
Code
// Block editor panel names => classic editor meta box IDs to hide.
$wpide_hidden_panels = array(
'discussion-panel' => array( 'commentstatusdiv', 'commentsdiv', 'trackbacksdiv' ),
'post-link' => array( 'slugdiv' ),
'page-attributes' => array( 'pageparentdiv' ),
);
// Classic editor meta boxes without a block editor panel.
$wpide_hidden_meta_boxes = array( 'postcustom' );
add_action( 'add_meta_boxes', function ( $post_type ) use ( $wpide_hidden_panels, $wpide_hidden_meta_boxes ) {
if ( current_user_can( 'manage_options' ) ) {
return;
}
$boxes = $wpide_hidden_meta_boxes;
foreach ( $wpide_hidden_panels as $ids ) {
$boxes = array_merge( $boxes, $ids );
}
foreach ( $boxes as $box ) {
foreach ( array( 'normal', 'advanced', 'side' ) as $context ) {
remove_meta_box( $box, $post_type, $context );
}
}
}, 99 );
add_action( 'enqueue_block_editor_assets', function () use ( $wpide_hidden_panels ) {
if ( current_user_can( 'manage_options' ) ) {
return;
}
$script = sprintf(
'wp.domReady(function(){var s=wp.data.dispatch("core/editor");if(!s||!s.removeEditorPanel){s=wp.data.dispatch("core/edit-post");}%s.forEach(function(p){s.removeEditorPanel(p);});});',
wp_json_encode( array_keys( $wpide_hidden_panels ) )
);
wp_add_inline_script( 'wp-edit-post', $script );
} );
Notes
Edit the two lists to choose which panels stay hidden.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.