phpwordpresswordpress-gutenberggutenberg-blocksheading

How change the default value of the heading block in Gutenberg


Default H2 when start new heading

The default heading is H2, but I don´t have this options, I only have H3 in dropdown

How change the default value of heading, for example change to H3?

I removed the headings from the Gutenberg editor when selecting a title with H1 to H6 and only kept the H3.
When I create a new title, the default H2 always appears. I wish I could change this in my case to a default heading H3.

function define_editor_heading_levels( $args, $block_type ) {
    
    if ( 'core/heading' !== $block_type ) {
        return $args;
    }

    // Remove H1, H2, H4, H5 and H6
    $args['attributes']['levelOptions']['default'] = [ 3 ];
    
    return $args;
}
add_filter( 'register_block_type_args', 'define_editor_heading_levels', 10, 2 );

Solution

  • I added the correct code to define a default heading value in the code below:

    function define_editor_heading_levels( $args, $block_type ) {
        if ( 'core/heading' !== $block_type ) {
            return $args;
        }
    
        // Change default heading level value to 3
        $args['attributes']['level']['default'] = 3;
    
        // Keep only H3 level 3 option
        $args['attributes']['levelOptions']['default'] = [ 3 ];
        
        return $args;
    }
    add_filter( 'register_block_type_args', 'define_editor_heading_levels', 10, 2 );
    

    Now, when adding a heading block in the Gutenberg Editor, you will get a heading level 3 by default (instead of a level 2).

    Redefined default heading value:

    Heading block Default value

    Heading option available levels:

    Heading block options