Block Editor PHP Intermediate

Limit Heading Levels in the Heading Block

Only offer H2 to H4 in the Heading block, so editors can't add a second H1 or tiny H6 headings.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 6 lines
add_filter( 'register_block_type_args', function ( array $args, string $block_type ): array {
	if ( 'core/heading' === $block_type ) {
		$args['attributes']['levelOptions']['default'] = [ 2, 3, 4 ];
	}
	return $args;
}, 10, 2 );

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.

Your theme already prints the page title as the H1, so an H1 inside the content creates a second one. H5 and H6 are rarely needed. This snippet limits the Heading block’s level picker to H2, H3 and H4.

How it works

Since WordPress 6.7, the Heading block has a levelOptions attribute that sets which levels appear in its toolbar. The register_block_type_args filter changes the block’s settings as it’s registered, setting the default options to 2, 3 and 4 for every new Heading block.

Things to watch

  • Existing headings keep their level, even if it’s no longer in the list.
  • Editors can still change a block’s level options in the Code editor. Combine with “Hide the Code Editor From Non-Administrators” if that matters.

How to test it

Add a Heading block and open its level menu. Only H2, H3 and H4 should be available.

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