How do I disable wpautop for a specific block?

I've registered a custom editor block with Advanced Custom Fields (ACF), and am using render_template with a PHP template partial to display the block contents.

Inside the template is some HTML (section with a figure and an a inside it). When the block is called and the template generates the HTML, it's exactly as I author it, but when displayed on the front end, the a gets wrapped in a p tag, and a couple of br elements are added (I assume from wpautop()). This isn't happening on the editor side, just on the front end, which leads me to believe the block HTML is getting run through the_content or some other filters that run wpautop() before display.

I've tried running the block content through buffering, which breaks the editor but fixes the front end, and tried disabling wpautop from running in the_content filter, but have had mixed results.

So my question is how to tell WordPress that I like my markup, thank you very much, and please leave it alone for this specific block?

Here's a Gist of the block template: https://gist.github.com/morganestes/eca76cf8490f7b943d2f44c75674b648.

Topic block-editor post-editor wp-autop Wordpress

Category Web


@morgan-estes Great solution but it i think it should be better to add_filter in else so the next block or content gets filter with WPAUTOP

/**
 * Try to disable wpautop inside specific blocks.
 *
 * @link https://wordpress.stackexchange.com/q/321662/26317
 *
 * @param string $block_content The HTML generated for the block.
 * @param array  $block         The block.
 */
add_filter( 'render_block', function ( $block_content, $block ) {
    if ( 'acf/featured-pages' === $block['blockName'] ) {
        remove_filter( 'the_content', 'wpautop' );
    } elseif ( ! has_filter( 'the_content', 'wpautop' ) ) {
        add_filter( 'the_content', 'wpautop' );
    }

    return $block_content;
}, 10, 2 );

I've found that I can use the render_block filter to disable wpautop() when a block gets rendered. It looks to me that this only affects the immediate block, as the filter gets re-enabled from do_blocks() calling _restore_wpautop_hook(). This lets me avoid using output buffering inside the block template, and moves the logic for filtering output out of the template and into a filter callback, where it makes more sense.

/**
 * Try to disable wpautop inside specific blocks.
 *
 * @link https://wordpress.stackexchange.com/q/321662/26317
 *
 * @param string $block_content The HTML generated for the block.
 * @param array  $block         The block.
 */
add_filter( 'render_block', function ( $block_content, $block ) {
    if ( 'acf/featured-pages' === $block['blockName'] ) {
        remove_filter( 'the_content', 'wpautop' );
    }

    return $block_content;
}, 10, 2 );

I'm not sure of all the ramifications of this, but it's working to solve my immediate problem.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.