Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.