wp_editor()
Displays the text editor - Quicktags (textarea with buttons) and TinyMCE (visual editor). Can be used in templates, on pages like: post, static page, etc.
NOTE: After initializing the TinyMCE editor, it cannot be safely moved in the DOM. For this reason, calling wp_editor() inside a meta box is not the best idea (this is suitable when only the Quicktags editor is needed).
On the post edit screen, several events (hooks) can be used to enable additional editors with TinyMCE: edit_page_form, edit_form_advanced, and dbx_post_sidebar. See https://core.trac.wordpress.org/ticket/19173.
_WP_Editors should not be used directly. See https://core.trac.wordpress.org/ticket/17144.
Note: The textarea field is filled with the content entered in the visual editor upon form submission, in which the editor is wrapped. If you need to get the entered content before submission, for example during an AJAX request, you will have to do this manually in JS, through the global variable tinyMCE.
We will get the content unprocessed, as it is - with <p> tags, etc.
let content = window.tinyMCE.get('editor_id').getContent()
// The content in this case will be obtained with all HTML tags:
// <p>Content written for <strong>test</strong></p>
If you need to get the content processed, as it usually is, then first call the event tinyMCE.triggerSave(), and then take the value of the textarea field:
window.tinyMCE.triggerSave()
let content = jQuery('#editor_id').val()
// The content in this case will be processed - unnecessary HTML tags will be removed:
// Content written for <strong>test</strong>No Hooks.
Returns
null. Outputs the HTML code of the form.
Template usage
wp_editor( 'content', 'editorid', array( 'wpautop' => 1, 'media_buttons' => 1, 'textarea_name' => '', //must be specified! 'textarea_rows' => 20, 'tabindex' => null, 'editor_css' => '', 'editor_class' => '', 'teeny' => 0, 'dfw' => 0, 'tinymce' => 1, 'quicktags' => 1, 'drag_drop_upload' => false ) );
Usage
wp_editor( $content, $editor_id, $settings = array() );
- $content(string) (required)
- Predefined text in the form field.
- $editor_id(string) (required)
Identifier for the textarea and TinyMCE fields.
IMPORTANT! Can only contain lowercase letters
[a-z]. Hyphens (-), underscores (_) cannot be used, for example:editpost,mycustomeditor.- $settings(array)
- Array of arguments (see below).
Default: presets
Arguments of the $settings parameter
- wpautop(boolean)
- 1 - apply the function wpautop().
Default: 1 - media_buttons(boolean)
- Show media button (insert/upload).
Default: 1 - textarea_name(string)
- value of the name attribute for the textarea field.
Default: $editor_id - textarea_rows(number)
- Number of rows in the form field. Height of the text input field. By default, the value is taken from the admin panel settings.
Default: get_option('default_post_edit_rows', 10) - tabindex(number)
- Value of the tabindex attribute for the textarea field.
Default: 0 - editor_css(string)
- Additional CSS styles for the editor. Must be specified in the
<style>tag. The text specified here will be placed right before the form.
Default: '' - editor_class(string)
- Additional CSS classes for the textarea field.
Default: '' - teeny(boolean)
- Show a reduced editor or a full one. In the reduced version, the button for the additional second panel will be hidden. Special filters and events will also work for it. 1 - will hide the button.
- dfw(boolean)
- Replace the default fullscreen mode with DFW (requires special DOM elements and CSS).
- tinymce(boolean/array)
Load the TinyMCE visual editor or not. You can specify editor parameters directly in the
array(). Possible array elements: from the file: /wp-includes/class-wp-editor.php:$set['tinymce']['wp_autoresize_on'] // then $mceInit = array ( 'selector' => "#$editor_id", 'resize' => 'vertical', 'menubar' => false, 'wpautop' => (bool) $set['wpautop'], 'indent' => ! $set['wpautop'], 'toolbar1' => implode( ',', $mce_buttons ), 'toolbar2' => implode( ',', $mce_buttons_2 ), 'toolbar3' => implode( ',', $mce_buttons_3 ), 'toolbar4' => implode( ',', $mce_buttons_4 ), 'tabfocus_elements' => $set['tabfocus_elements'], 'body_class' => $body_class ); // Merge with the first part of the init array $mceInit = array_merge( self::$first_init, $mceInit ); if ( is_array( $set['tinymce'] ) ) $mceInit = array_merge( $mceInit, $set['tinymce'] ); // where $mce_buttons looks like this $mce_buttons = [ 'formatselect', 'bold', 'italic', 'strikethrough', 'bullist', 'numlist', 'blockquote', 'hr', 'alignleft', 'aligncenter', 'alignright', 'link', 'unlink', 'wp_more', 'spellchecker' ]; // and additional buttons in the array: 'fullscreen', 'wp_adv' // And this is how the second button panel looks $mce_buttons_2 = [ 'strikethrough', 'hr', 'forecolor', 'pastetext', 'removeformat', 'charmap', 'outdent', 'indent', 'undo', 'redo', 'wp_help' ];
Also, the button set can be changed through filters. Here are all the filters that are responsible for the button set:
// If the teeny option is set (reduced) $mce_teeny = [ 'bold', 'italic', 'underline', 'blockquote', 'strikethrough', 'bullist', 'numlist', 'alignleft', 'aligncenter', 'alignright', 'undo', 'redo', 'link', 'unlink', 'fullscreen' ]; $mce_buttons = apply_filters( 'teeny_mce_buttons', $mce_teeny, $editor_id ); // If the full editor $mce_buttons = apply_filters( 'mce_buttons', $mce_buttons, $editor_id ); $mce_buttons_2 = apply_filters( 'mce_buttons_2', $mce_buttons_2, $editor_id ); $mce_buttons_3 = apply_filters( 'mce_buttons_3', array(), $editor_id ); $mce_buttons_4 = apply_filters( 'mce_buttons_4', array(), $editor_id );
If an empty array is returned, the default buttons will be shown, not the absence of buttons, as may seem at first glance.
Default: 1
- quicktags(boolean/array)
Load the HTML editor or not. You can specify parameters directly in the
array(). Possible array elements:array( 'id' => $editor_id, 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,more,close,fullscreen' );
Default: 1
- drag_drop_upload(boolean)
- Enables Drag-and-Drop support when uploading files (since WordPress 3.9)
Default: false
Examples
#1 Changing form settings
We can also change the output form settings if we don't like the basic ones. For example, we can remove the add media button:
$settings = array( 'media_buttons' => false ); wp_editor( $content, $editor_id, $settings );
#2 TinyMCE on the pages of the site
Let's display the text input field with the TinyMCE editor and HTML editor somewhere on the pages of the site.
To do this, insert the following code in the template file, in the place where you want to display such a field:
<?php wp_editor( '', 'wpeditor', [ 'textarea_name' => 'content' ] ); ?>
#3 Let's fill in the field with the content of post 51:
$post_id = 51; $post = get_post( $post_id, OBJECT, 'edit' ); $content = $post->post_content; $editor_id = 'editpost'; wp_editor( $content, $editor_id );
Notes
Changelog
| Since 3.3.0 | Introduced. |
wp_editor() wp editor code WP 6.9
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 );
}