format_for_editor()
云策文档标注
概述
format_for_editor() 函数用于格式化文本以供编辑器使用,主要对文本进行 HTML 实体编码处理,并应用 'format_for_editor' 过滤器。
关键要点
- 函数将文本中的特殊字符(如 <, >, &)转换为 HTML 实体,以防止在 textarea 中显示异常。
- 通过 apply_filters() 应用 'format_for_editor' 过滤器,允许开发者自定义格式化后的文本。
- 参数 $default_editor 可选,表示当前用户的默认编辑器(如 'html' 或 'tinymce'),默认为 null。
- 函数返回经过过滤的格式化文本字符串。
代码示例
function format_for_editor( $text, $default_editor = null ) {
if ( $text ) {
$text = htmlspecialchars( $text, ENT_NOQUOTES, get_option( 'blog_charset' ) );
}
/**
* Filters the text after it is formatted for the editor.
*
* @since 4.3.0
*
* @param string $text The formatted text.
* @param string $default_editor The default editor for the current user.
* It is usually either 'html' or 'tinymce'.
*/
return apply_filters( 'format_for_editor', $text, $default_editor );
}注意事项
- 如果 $text 为空,过滤器将应用于空字符串。
- 函数自 WordPress 4.3.0 版本引入。
- 相关函数包括 apply_filters() 和 get_option(),用于调用过滤器和获取博客字符集选项。
原文内容
Formats text for the editor.
Description
Generally the browsers treat everything inside a textarea as text, but it is still a good idea to HTML entity encode <, > and & in the content.
The filter ‘format_for_editor’ is applied here. If $text is empty the filter will be applied to an empty string.
See also
Parameters
$textstringrequired-
The text to be formatted.
$default_editorstringoptional-
The default editor for the current user.
It is usually either'html'or'tinymce'.Default:
null
Source
function format_for_editor( $text, $default_editor = null ) {
if ( $text ) {
$text = htmlspecialchars( $text, ENT_NOQUOTES, get_option( 'blog_charset' ) );
}
/**
* Filters the text after it is formatted for the editor.
*
* @since 4.3.0
*
* @param string $text The formatted text.
* @param string $default_editor The default editor for the current user.
* It is usually either 'html' or 'tinymce'.
*/
return apply_filters( 'format_for_editor', $text, $default_editor );
}
Hooks
- apply_filters( ‘format_for_editor’, string $text, string $default_editor )
-
Filters the text after it is formatted for the editor.
Changelog
| Version | Description |
|---|---|
| 4.3.0 | Introduced. |