$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>post editor - How do I disable wpautop for a specific block?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

post editor - How do I disable wpautop for a specific block?

matteradmin8PV0评论

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: .

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/morganestes/eca76cf8490f7b943d2f44c75674b648.

Share Improve this question asked Dec 11, 2018 at 16:55 Morgan EstesMorgan Estes 1,55512 silver badges22 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

@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/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/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.

Post a comment

comment list (0)

  1. No comments so far