Gutenberg 自定义块 php 渲染问题

Gutenberg custom blocks php render issue

我正在为 WordPress Gutenberg 编辑器创建一些自定义动态块(在 link 之后)。

我对这些块使用 PHP 渲染,这意味着我保存了以下代码:

save: function( props ) {
    // Rendering in PHP
      return;

},

通过此回调调用渲染函数:

register_block_type( 'my-plugin/latest-post', array(
    'render_callback' => 'my_plugin_render_block_latest_post',
) );

我不会 post 函数代码,因为在这种情况下无关紧要。 (我愿意 a WP_Query 并显示一些自定义 post 数据和 return a html 代码),

我的问题是 WP Gutenberg 获取函数的输出并添加 <p> and <br> 标签(经典的 wpautop 行为)。

我的问题是:如何只对自定义块禁用它?我可以用这个:

remove_filter( 'the_content', 'wpautop' );

但我不想更改默认行为。

一些额外的发现。用于块渲染的 php 函数使用 get_the_excerpt()。一旦使用了这个函数(我假设 get_the_content() 正在发生) wpautop 过滤器被应用并且块的 html 标记被弄乱了。

我不知道这是错误还是预期的行为,但是是否有任何不涉及删除过滤器的简单解决方案? (对于 themeforest 上的 ex,不允许删除此过滤器。)

我们默认有:

add_filter( 'the_content', 'do_blocks', 9 );
add_filter( 'the_content', 'wpautop' );
add_filter( 'the_excerpt', 'wpautop' );
...

我浏览了 do_blocks() (src),如果我理解正确,如果内容包含任何块,它会删除 wpautop 过滤,但会为任何后续 the_content() 用法。

我想知道您的渲染块回调是否包含任何此类后续用法,正如您提到的 WP_Query 循环。

例如,尝试:

$block_content = '';

remove_filter( 'the_content', 'wpautop' ); // Remove the filter on the content.
remove_filter( 'the_excerpt', 'wpautop' ); // Remove the filter on the excerpt.

... code in callback ...

add_filter( 'the_content', 'wpautop' );    // Restore the filter on the content.
add_filter( 'the_excerpt', 'wpautop' );    // Restore the filter on the excerpt.

return $block_content;

在您的 my_plugin_render_block_latest_post() 回调代码中。