Limit Heading Levels in the Editor
Shows only the heading levels you allow (for example H2 to H4) in the Heading block toolbar, so editors keep a clean document outline and never add a second H1.
Opens WPIDE on
Code
// Heading levels to offer.
$wpide_heading_levels = array( 2, 3, 4 );
add_filter( 'register_block_type_args', function ( $args, $block_type ) use ( $wpide_heading_levels ) {
if ( 'core/heading' !== $block_type || ! isset( $args['attributes'] ) ) {
return $args;
}
$args['attributes']['levelOptions'] = array(
'type' => 'array',
'default' => array_map( 'intval', $wpide_heading_levels ),
);
return $args;
}, 10, 2 );
Notes
Requires WordPress 6.7 or newer. Existing headings keep their level.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.