Block Editor PHP Beginner

Hide the Code Editor From Non-Administrators

Remove the "Code editor" option from the block editor for everyone except administrators.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 6 lines
add_filter( 'block_editor_settings_all', function ( array $settings ): array {
	if ( ! current_user_can( 'manage_options' ) ) {
		$settings['codeEditingEnabled'] = false;
	}
	return $settings;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

The block editor’s Code editor shows a post’s raw block markup. Editing it by hand is an easy way to break blocks. This snippet removes the option for non-administrators.

How it works

codeEditingEnabled in the editor settings controls whether the “Code editor” switch appears in the Options menu. The snippet sets it to false for users without manage_options.

Things to watch

  • Individual blocks can still be switched to HTML editing from their own menu. That’s controlled separately by each block’s html support.

How to test it

Log in as an editor, open the editor’s Options menu, and check that “Code editor” is missing.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only