wp_editor()
概述
wp_editor() 函数用于渲染一个编辑器,支持 TinyMCE 和 Quicktags,是输出编辑器组件的标准方法。开发者应避免直接使用 _WP_Editors 类,并注意编辑器初始化后不能在 DOM 中安全移动。
关键要点
- 函数用途:渲染编辑器,包含 TinyMCE 和 Quicktags 所需组件。
- 参数说明:$content(初始内容,字符串,必需)、$editor_id(HTML ID,字符串,必需,不能包含方括号)、$settings(设置数组,可选,默认空数组)。
- 注意事项:TinyMCE 编辑器初始化后不能安全移动 DOM,因此不建议在 meta box 中使用(除非仅用 Quicktags)。
- 相关函数:_WP_Editors::editor() 用于输出编辑器 HTML,_WP_Editors::parse_settings() 用于解析设置。
- 版本历史:自 WordPress 3.3.0 引入。
代码示例
// 基本用法:显示空编辑器
$content = '';
$editor_id = 'mycustomeditor';
wp_editor( $content, $editor_id );
// 自定义设置:隐藏媒体按钮
$settings = array( 'media_buttons' => false );
wp_editor( $content, $editor_id, $settings );
// 填充特定文章内容
$post = get_post( 51, OBJECT, 'edit' );
$content = $post->post_content;
wp_editor( $content, 'editpost' );
// 高级设置:自定义工具栏和移除按钮
$args = array(
'media_buttons' => false,
'textarea_name' => 'editorname',
'quicktags' => false,
'tinymce' => array(
'toolbar1' => 'bold,italic,underline,link,unlink'
)
);
wp_editor( '', 'editorid', $args );注意事项
- 避免在 meta box 中使用 wp_editor() 包含 TinyMCE,除非仅使用 Quicktags,以防止 DOM 移动问题。
- 在文章编辑屏幕,可使用动作如 'edit_page_form' 来添加额外编辑器。
- 设置数组的详细描述参考 _WP_Editors::parse_settings() 方法。
- 从 WordPress 4.8 开始,可通过 JavaScript 直接调用编辑器 API。
Renders an editor.
Description
Using this function is the proper way to output all needed components for both TinyMCE and Quicktags.
_WP_Editors should not be used directly. See https://core.trac.wordpress.org/ticket/17144.
NOTE: Once initialized the TinyMCE editor cannot be safely moved in the DOM. For that reason running wp_editor() inside of a meta box is not a good idea unless only Quicktags is used.
On the post edit screen several actions can be used to include additional editors containing TinyMCE: ‘edit_page_form’, ‘edit_form_advanced’ and ‘dbx_post_sidebar’.
See https://core.trac.wordpress.org/ticket/19173 for more information.
See also
Parameters
$contentstringrequired-
Initial content for the editor.
$editor_idstringrequired-
HTML ID attribute value for the textarea and TinyMCE.
Should not contain square brackets. $settingsarrayoptional-
See _WP_Editors::parse_settings() for description.
Default:
array()
Source
function wp_editor( $content, $editor_id, $settings = array() ) {
if ( ! class_exists( '_WP_Editors', false ) ) {
require ABSPATH . WPINC . '/class-wp-editor.php';
}
_WP_Editors::editor( $content, $editor_id, $settings );
}
Changelog
| Version | Description |
|---|---|
| 3.3.0 | Introduced. |
Skip to note 8 content
M A Vinoth Kumar
Get the wp_editor through AJAX Call,
add_action( 'wp_ajax_bc_ajax_request', 'bc_ajax_request_fn' ); function bc_ajax_request_fn(){ $html =''; $html .= bc_get_wp_editor('Test Message','primary_editor',array()); return $html; }function bc_get_wp_editor( $content = '', $editor_id, $options = array() ) { ob_start(); wp_editor( $content, $editor_id, $options ); $temp = ob_get_clean(); $temp .= _WP_Editors::enqueue_scripts(); $temp .= print_footer_scripts(); $temp .= _WP_Editors::editor_js(); return $temp; }Skip to note 9 content
Mário Valney
To edit tinymce Visual Buttons, you should use
toolbarinstead of TinyMCE documentation’stheme_advanced_buttonsattribute:$args = array( 'tinymce' => array( 'toolbar1' => 'bold,italic,underline,separator,alignleft,aligncenter,alignright,separator,link,unlink,undo,redo', 'toolbar2' => '', 'toolbar3' => '', ), ); wp_editor( $content, $editor_id, $args );Skip to note 10 content
Ricardo Moraleida
The $settings array is actually described on the [_WP_Editors::parse_settings()](https://developer.wordpress.org/reference/classes/_wp_editors/parse_settings/) method
Skip to note 11 content
Codex
Modify the editor’s default settings when initializing it
You can pass an array of one or more settings to modify for this editor instance, such as hiding the insert media buttons.
$content = ''; $editor_id = 'mycustomeditor'; $settings = array( 'media_buttons' => false ); wp_editor( $content, $editor_id, $settings );Skip to note 12 content
Codex
Display an empty editor using the default settings
$content = ''; $editor_id = 'mycustomeditor'; wp_editor( $content, $editor_id );Skip to note 13 content
Codex
Fill an editor with the content for a particular post
$post_id = 51; $post = get_post( $post_id, OBJECT, 'edit' ); $content = $post->post_content; $editor_id = 'editpost'; wp_editor( $content, $editor_id );Skip to note 14 content
Mustafa BOŞNAK
Remove HTML button and media button in editor. Create blank content. Define a custom name and id for the editor. Determine your editor’s row count.
$content = ""; $custom_editor_id = "editorid"; $custom_editor_name = "editorname"; $args = array( 'media_buttons' => false, // This setting removes the media button. 'textarea_name' => $custom_editor_name, // Set custom name. 'textarea_rows' => get_option('default_post_edit_rows', 10), //Determine the number of rows. 'quicktags' => false, // Remove view as HTML button. ); wp_editor( $content, $custom_editor_id, $args );