Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render_block -> core/heading ["attrs"]["level"] only with <h1> headings #16877

Closed
nicmare opened this issue Aug 2, 2019 · 5 comments
Closed
Labels
[Block] Heading Affects the Headings Block Needs Testing Needs further testing to be confirmed.

Comments

@nicmare
Copy link

nicmare commented Aug 2, 2019

When i debug the render_block filter on core/heading block, i've noticed there is an attrs array with "level" entry for only h1 headline. Then "level" has value of "1". When i change h1 to h2 or h3 the "level" entry is missing. Is this on purpose?! WordPress 5.2.2

@swissspidy swissspidy added [Block] Heading Affects the Headings Block Needs Testing Needs further testing to be confirmed. labels Aug 2, 2019
@shawncook
Copy link

Noticing this as well. I'm able to target core/heading blocks with a level of 1, but I can't target 2 through 6.

@danbowles
Copy link

danbowles commented Oct 10, 2019

Ended up implementing a regex:

if (
	'core/heading' === $block['blockName']
) {
	preg_match( '/\n*<(?P<level>h[1-6])/si', $block['innerHTML'], $matches );
	$heading_level = ! empty( $matches['level'] ) ? 'block--heading-' . $matches['level'] : '';
	$wrapped_block_content = sprintf(
		'<div class="block--heading-container '. $heading_level .'">%s</div>',
		$block_content
	);
}
return ! empty( $wrapped_block_content ) ? $wrapped_block_content : $block_content;

This will produce:
<div class="block--heading-container block--heading-h1/h2/etc">

@certainlyakey
Copy link

certainlyakey commented Oct 13, 2019

Clearly a bug. Makes creating a table of contents or page headings menu more difficult.

This is even more strange considering that by default Gutenberg only displays h2-h4 as level options in the floating toolbar (assuming that h1 should not really be used within post content).

@talldan
Copy link
Contributor

talldan commented Apr 30, 2020

I'm not able to reproduce fully what's described using the following code:

function my_test_render_callback( $content, $block ) {
	if ( 'core/heading' !== $block['blockName'] ) {
		return $content;
	}

	return json_encode( $block[ 'attrs' ] );
}

add_filter( 'render_block', 'my_test_render_callback', 10, 2 );

I do indeed see that there's no level attribute when the level is set to 2, but at other levels it works ok.

This is happening because the default level is 2, and default attributes aren't applied for static blocks when trying to access them in PHP. It looks like this is tracked in #2751, I think there's a long term effort to improve this, part of which is moving all the attribute definitions to .json files so that they can be read by PHP.

I'll close this if that's ok, as I think this is adequately tracked in #2751.

@swinggraphics
Copy link

Default attributes aren't applied for static blocks when trying to access them in PHP

Aaaargh. Gutenberg: making life harder in PHP, and requiring a lot more skill and hoop-jumping to access in JS. The linked issue #2751 is Closed, as well. 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Heading Affects the Headings Block Needs Testing Needs further testing to be confirmed.
Projects
None yet
Development

No branches or pull requests

7 participants