0

Whenever I attempt to define a level other than H2 for 'core/heading' in any block template, upon reload of the page editor, the block gets a "This block contains unexpected or invalid content" error, and when the "Attempt Block Recovery" button is clicked, the block level is changed to level 2. Why would I not seem to be able to use any other level than 2 in a block template?

Example template excerpt:

<?php
$template = [
  [
        'core/heading', 
        [
          'level' => '2',
            'content' => 'Directions',
            'className' => 'block-header-directions',
        ],
    ],
  [
        'core/heading', 
        [
          'level' => '3',
            'placeholder' => 'Subheading',
            'className' => 'block-subheader-directions has-blue-alt-color',
        ],
    ],
  [
        'core/list', 
        [
          'placeholder' => 'List item',
          'ordered' => true,
          'className' => 'block-list-directions',
        ],
  ],
];

In the above example, the block with the placeholder "Subheading" causes that chain of events.

I thought initially that this was potentially some enforcement of semantics (that Gutenberg wanted to see H1, H2, H3, etc. in the correct order in the content), but it occurs, as in the case above, even when the heading levels are in a completely sematic order in the content.

(Potentially related, as the forced H2 problem seems to extend to other editor areas as well: I've added a block style to 'core/heading', so the block editor now displays previews of the styles. But no matter which heading level is selected, only the preview for the H2 heading is displayed.)

3

A new day, and refreshed Google-fu powers provided the simple answer: The level attribute needs to be an integer, not a a string.

[
  'core/heading', 
  [
     'level' => 3,
     'placeholder' => 'Subheading',
     'className' => 'block-subheader-directions has-blue-alt-color',
  ],
]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.