Disable Gutenberg Code Editing for Non-Admin Users

This WordPress code snippet disables the Gutenberg code editing feature for non-admin users. Gutenberg is the default WordPress block editor introduced in WordPress 5.0. While it provides a great user experience, there are situations where you may not want all users to have access to all of its features, such as code editing.

The code uses the ‘block_editor_settings_all’ filter, which allows you to customize the settings of the Gutenberg block editor. It sets the ‘codeEditingEnabled’ setting to the result of the current_user_can( 'manage_options' ) function.

The current_user_can( 'manage_options' ) function checks whether the current user has the ‘manage_options’ capability, which is typically only assigned to administrators. As a result, if the current user is an administrator, ‘codeEditingEnabled’ will be true, otherwise, it will be false.

In other words, this snippet enables Gutenberg code editing for administrators and disables it for all other users.

This snippet does not require any modification unless you want to change the capability that determines whether code editing is enabled. Just remember that limiting functionality like this could affect the user experience for non-admin users.

PHP
add_filter( 'block_editor_settings_all', function ( $settings ) {
	
	$settings['codeEditingEnabled'] = current_user_can( 'manage_options' );
	return $settings;
} );