I have a a custom block that is dynamic and rendered using the render callback.
The custom block renders the excerpt.
The render callback returns something like this:
<div>
<a href="http://example.com"><?php echo get_the_excerpt(); ?></a>
</div>
Prior to Gutenberg 4.6 this worked as expected. After updating, I now get paragraph tags added around the link.
Digging into this a bit - it seems the regression was caused by this change: https://github.com/WordPress/gutenberg/commit/2a66db0fc99a9fb1ba99f61e59e6a0b2a4a5f9ef#diff-6ff32417da0658502e7caa1a1abbeae6
The key difference is the new code restores the wpautop filter at a later priority, which is still getting run.
Digging into this some more - it is actually because get_the_excerpt internally calls apply_filters( 'the_content' - which is a bit of a headache.
To Reproduce
The small test case below will reproduce the issue on the Gutenberg master.
add_action( 'after_setup_theme', function() {
register_block_type( 'test/autopbug', [
'render_callback' => 'test_autop_bug_render_callback',
] );
} );
function test_autop_bug_render_callback() {
ob_start();
?>
<div class="test-autop-bug">
<a href="http://example.com"><?php echo get_the_excerpt(); ?></a>
</div>
<?php return ob_get_clean();
}
Then add the following block to a posts content using the code editor:
<!-- wp:test/autopbug /-->
Then on the front end inspect the markup. The link will be wrapped in a paragraph. If you remove the call to get_the_excerpt then the link is not wrapped in a paragraph.
Does this issue reproduce with 5.0-RC3 too, or simply the plugin?
I鈥檒l try and test with 5.0-RC3
@danielbachhuber Confirmed this is an issue with 5.0-RC3
@mattheu Ok. Can you create a Core Trac ticket and we'll get this fixed in 5.0.x? Most of the PHP code in Gutenberg is going away once 5.0 ships.
This is still reproducible with WP version 5.0.3, using get_the_excerpt() inside a server rendered block
Maybe related to #12530? There's a workaraound mentioned in https://github.com/WordPress/gutenberg/issues/12530#issuecomment-443736873.
Another potential workaround - if you present the HTML as a core/html block and run it through do_blocks() then wpautop doesn't seem to be applied:
function test_autop_bug_render_callback() {
ob_start();
?>
<div class="test-autop-bug">
<a href="http://example.com"><?php echo get_the_excerpt(); ?></a>
</div>
<?php return do_blocks('<!-- wp:html -->' . ob_get_clean() . '<!-- /wp:html -->');
}
Not sure if that has any side effects.
I'm also seeing this issue in WordPress 5.1.1, but it does look like there's a ticket open on Trac at https://core.trac.wordpress.org/ticket/45495 to deal with this bug.
I see this too - strangely on some sites and not others. On one site it works correctly on staging, but the <p> tags are appearing in custom blocks on the production site :( It also adds <br> in unwanted places e.g. between an image wrapped in an anchor.
Most helpful comment
Another potential workaround - if you present the HTML as a
core/htmlblock and run it throughdo_blocks()then wpautop doesn't seem to be applied:Not sure if that has any side effects.