Block Editor PHP Intermediate

Lock the Site Editor and Template Editing

Hide the Site Editor and turn off template editing in the post editor, so clients can write content without changing headers, footers or templates.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 29 lines
/**
 * Lock the Site Editor and template editing.
 * To unlock temporarily, add define( 'NAHNU_ALLOW_SITE_EDITOR', true ); to wp-config.php.
 */
function nahnu_site_editor_locked(): bool {
	return ! ( defined( 'NAHNU_ALLOW_SITE_EDITOR' ) && NAHNU_ALLOW_SITE_EDITOR );
}

// Remove the Appearance > Editor menu item.
add_action( 'admin_menu', function (): void {
	if ( nahnu_site_editor_locked() ) {
		remove_submenu_page( 'themes.php', 'site-editor.php' );
	}
}, 999 );

// Block direct visits to the Site Editor.
add_action( 'load-site-editor.php', function (): void {
	if ( nahnu_site_editor_locked() ) {
		wp_die( 'The Site Editor is turned off on this site.', 'Site Editor turned off', [ 'response' => 403, 'back_link' => true ] );
	}
} );

// Remove the "Edit template" option from the post editor.
add_filter( 'block_editor_settings_all', function ( array $settings ): array {
	if ( nahnu_site_editor_locked() ) {
		$settings['supportsTemplateMode'] = 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.

On block themes, anyone who can edit theme options can open the Site Editor and change headers, footers and templates, and post editors can jump into template editing from the sidebar. That’s great while building a site and risky once a client takes over. This snippet locks those areas until you deliberately unlock them.

How it works

  • The admin_menu callback removes Appearance → Editor.
  • The load-site-editor.php action runs just before the Site Editor loads and stops it with a clear message, so bookmarks and direct links don’t get around the menu change.
  • The block_editor_settings_all filter sets supportsTemplateMode to false, which removes the template options from the post and page editor.

The old version of this snippet hooked a filter that only exists in the Gutenberg plugin, so it did nothing, and it called theme-support functions outside any hook. This version only uses stable core hooks.

Unlocking it

When you need to make design changes, add define( 'NAHNU_ALLOW_SITE_EDITOR', true ); to wp-config.php. Everything comes back. Remove the line when you’re done.

Things to watch

This doesn’t change what’s already built. Templates, template parts and global styles you saved keep working. It also doesn’t remove the Styles panel for individual blocks; to restrict that, combine it with the snippet that limits allowed blocks, or a theme.json that disables custom colours and font sizes.

How to test it

Go to Appearance. The Editor item should be gone, and visiting /wp-admin/site-editor.php should show the “turned off” message.

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