函数文档

wp_default_editor()

💡 云策文档标注

概述

wp_default_editor() 函数用于确定应默认显示哪个编辑器给用户,支持 'tinymce'、'html' 或 'test' 返回值。它基于用户能力和设置,并可通过过滤器自定义。

关键要点

  • 函数返回字符串:'tinymce'、'html' 或 'test',其中 'html' 对应“代码”编辑器标签。
  • 默认逻辑:如果用户能使用富文本编辑器(user_can_richedit()),则返回 'tinymce',否则返回 'html'。
  • 用户设置优先:如果当前用户存在,会检查 get_user_setting('editor') 的值,若有效则覆盖默认值。
  • 可过滤性:通过 apply_filters('wp_default_editor', $r) 钩子,允许开发者修改默认编辑器选择。

代码示例

function wp_default_editor() {
    $r = user_can_richedit() ? 'tinymce' : 'html'; // Defaults.
    if ( wp_get_current_user() ) { // Look for cookie.
        $ed = get_user_setting( 'editor', 'tinymce' );
        $r  = ( in_array( $ed, array( 'tinymce', 'html', 'test' ), true ) ) ? $ed : $r;
    }
    return apply_filters( 'wp_default_editor', $r );
}

注意事项

  • 函数自 WordPress 2.5.0 版本引入,是编辑器系统的基础组件。
  • 相关函数包括 wp_get_current_user()、user_can_richedit()、get_user_setting() 和 apply_filters(),用于支持其功能。
  • 在 _WP_Editors::editor() 中被调用,以输出编辑器 HTML。

📄 原文内容

Finds out which editor should be displayed by default.

Description

Works out which of the editors to display as the current editor for a user. The ‘html’ setting is for the “Code” editor tab.

Return

string Either 'tinymce', 'html', or 'test'

Source

function wp_default_editor() {
	$r = user_can_richedit() ? 'tinymce' : 'html'; // Defaults.
	if ( wp_get_current_user() ) { // Look for cookie.
		$ed = get_user_setting( 'editor', 'tinymce' );
		$r  = ( in_array( $ed, array( 'tinymce', 'html', 'test' ), true ) ) ? $ed : $r;
	}

	/**
	 * Filters which editor should be displayed by default.
	 *
	 * @since 2.5.0
	 *
	 * @param string $r Which editor should be displayed by default. Either 'tinymce', 'html', or 'test'.
	 */
	return apply_filters( 'wp_default_editor', $r );
}

Hooks

apply_filters( ‘wp_default_editor’, string $r )

Filters which editor should be displayed by default.

Changelog

Version Description
2.5.0 Introduced.