函数文档

format_to_edit()

💡 云策文档标注

概述

format_to_edit() 函数用于处理即将编辑的文本内容,通过应用 'format_to_edit' 过滤器并可选地使用 esc_textarea() 进行转义,确保文本在编辑界面中的安全显示。

关键要点

  • 函数接受两个参数:$content(必需,要编辑的文本)和 $rich_text(可选,布尔值,默认为 false,指示是否为富文本)。
  • 如果 $rich_text 为 false,文本会通过 esc_textarea() 转义,将特殊字符转换为 HTML 实体;否则直接返回过滤后的文本。
  • 函数返回经过过滤(和可能转义)的字符串,适用于 WordPress 编辑场景。
  • 相关函数包括 esc_textarea() 和 apply_filters(),用于文本转义和过滤器应用。
  • 从版本 4.4.0 起,参数 $richedit 更名为 $rich_text 以提高清晰度。

代码示例

function format_to_edit( $content, $rich_text = false ) {
    $content = apply_filters( 'format_to_edit', $content );
    if ( ! $rich_text ) {
        $content = esc_textarea( $content );
    }
    return $content;
}

注意事项

  • 在文档的第一段中,参数名 $richedit 应更正为 $rich_text,以反映版本更新后的命名。

📄 原文内容

Acts on text which is about to be edited.

Description

The $content is run through esc_textarea() , which uses htmlspecialchars() to convert special characters to HTML entities. If $richedit is set to true, it is simply a holder for the ‘format_to_edit’ filter.

Parameters

$contentstringrequired
The text about to be edited.
$rich_textbooloptional
Whether $content should be considered rich text, in which case it would not be passed through esc_textarea() .

Default:false

Return

string The text after the filter (and possibly htmlspecialchars()) has been run.

Source

function format_to_edit( $content, $rich_text = false ) {
	/**
	 * Filters the text to be formatted for editing.
	 *
	 * @since 1.2.0
	 *
	 * @param string $content The text, prior to formatting for editing.
	 */
	$content = apply_filters( 'format_to_edit', $content );
	if ( ! $rich_text ) {
		$content = esc_textarea( $content );
	}
	return $content;
}

Hooks

apply_filters( ‘format_to_edit’, string $content )

Filters the text to be formatted for editing.

Changelog

Version Description
4.4.0 The $richedit parameter was renamed to $rich_text for clarity.
0.71 Introduced.

User Contributed Notes