函数文档

_restore_wpautop_hook()

💡 云策文档标注

概述

_restore_wpautop_hook() 是一个 WordPress 钩子函数,用于在 do_blocks() 移除 wpautop() 后重新将其添加到 the_content 过滤器,确保后续内容处理正确。

关键要点

  • 函数作用:当 do_blocks() 需要从 the_content 过滤器中移除 wpautop() 时,此钩子会在之后重新添加 wpautop(),以支持后续的 the_content 使用。
  • 参数:接受一个字符串参数 $content,表示通过此过滤器的文章内容,并返回未修改的内容。
  • 实现逻辑:通过 has_filter() 检查当前优先级,然后使用 add_filter() 以较低优先级重新添加 wpautop(),并移除自身钩子。

代码示例

function _restore_wpautop_hook( $content ) {
    $current_priority = has_filter( 'the_content', '_restore_wpautop_hook' );

    add_filter( 'the_content', 'wpautop', $current_priority - 1 );
    remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );

    return $content;
}

注意事项

  • 此函数在 WordPress 5.0.0 版本中引入,主要用于处理 Gutenberg 块编辑器与 wpautop() 的兼容性问题。
  • 相关函数包括 has_filter()、add_filter() 和 remove_filter(),用于钩子管理。

📄 原文内容

If do_blocks() needs to remove wpautop() from the the_content filter, this re-adds it afterwards, for subsequent the_content usage.

Parameters

$contentstringrequired
The post content running through this filter.

Return

string The unmodified content.

Source

function _restore_wpautop_hook( $content ) {
	$current_priority = has_filter( 'the_content', '_restore_wpautop_hook' );

	add_filter( 'the_content', 'wpautop', $current_priority - 1 );
	remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );

	return $content;
}

Changelog

Version Description
5.0.0 Introduced.