Turn Off "Edit as HTML" on Blocks for Non-Administrators
Removes the "Edit as HTML" option from block menus for everyone except administrators, so authors cannot break block markup by hand.
Opens WPIDE on
Code
add_filter( 'block_type_metadata', function ( $metadata ) {
if ( current_user_can( 'manage_options' ) || empty( $metadata['name'] ) ) {
return $metadata;
}
$metadata['supports'] = isset( $metadata['supports'] ) ? (array) $metadata['supports'] : array();
$metadata['supports']['html'] = false;
return $metadata;
} );
Notes
Administrators keep the option. The Code editor mode is a separate setting.
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.