Hide the Site Editor and turn off template editing in the post editor, so clients can write content without changing headers, footers or templates.
/**
* 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;
} );
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.
admin_menu callback removes Appearance → Editor.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.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.
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.
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.
Go to Appearance. The Editor item should be gone, and visiting /wp-admin/site-editor.php should show the “turned off” message.