* Facilitates adding of the WordPress editor as used on the Write and Edit screens.
* Private, not included by default. See wp_editor() in wp-includes/general-template.php.
final class _WP_Editors {
public static $mce_locale;
private static $mce_settings = array();
private static $qt_settings = array();
private static $plugins = array();
private static $qt_buttons = array();
private static $ext_plugins;
private static $first_init;
private static $this_tinymce = false;
private static $this_quicktags = false;
private static $has_tinymce = false;
private static $has_quicktags = false;
private static $has_medialib = false;
private static $editor_buttons_css = true;
private static $drag_drop_upload = false;
private static $translation;
private static $tinymce_scripts_printed = false;
private static $link_dialog_printed = false;
private function __construct() {}
* Parse default arguments for the editor instance.
* @param string $editor_id HTML ID for the textarea and TinyMCE and Quicktags instances.
* Should not contain square brackets.
* @param array $settings {
* Array of editor arguments.
* @type bool $wpautop Whether to use wpautop(). Default true.
* @type bool $media_buttons Whether to show the Add Media/other media buttons.
* @type string $default_editor When both TinyMCE and Quicktags are used, set which
* editor is shown on page load. Default empty.
* @type bool $drag_drop_upload Whether to enable drag & drop on the editor uploading. Default false.
* Requires the media modal.
* @type string $textarea_name Give the textarea a unique name here. Square brackets
* can be used here. Default $editor_id.
* @type int $textarea_rows Number rows in the editor textarea. Default 20.
* @type string|int $tabindex Tabindex value to use. Default empty.
* @type string $tabfocus_elements The previous and next element ID to move the focus to
* when pressing the Tab key in TinyMCE. Default ':prev,:next'.
* @type string $editor_css Intended for extra styles for both Visual and Text editors.
* Should include `<style>` tags, and can use "scoped". Default empty.
* @type string $editor_class Extra classes to add to the editor textarea element. Default empty.
* @type bool $teeny Whether to output the minimal editor config. Examples include
* Press This and the Comment editor. Default false.
* @type bool $dfw Deprecated in 4.1. Unused.
* @type bool|array $tinymce Whether to load TinyMCE. Can be used to pass settings directly to
* TinyMCE using an array. Default true.
* @type bool|array $quicktags Whether to load Quicktags. Can be used to pass settings directly to
* Quicktags using an array. Default true.
* @return array Parsed arguments array.
public static function parse_settings( $editor_id, $settings ) {
* Filters the wp_editor() settings.
* @see _WP_Editors::parse_settings()
* @param array $settings Array of editor arguments.
* @param string $editor_id Unique editor identifier, e.g. 'content'. Accepts 'classic-block'
* when called from block editor's Classic block.
$settings = apply_filters( 'wp_editor_settings', $settings, $editor_id );
// Disable autop if the current post has blocks in it.
'wpautop' => ! has_blocks(),
'drag_drop_upload' => false,
'textarea_name' => $editor_id,
'tabfocus_elements' => ':prev,:next',
'_content_editor_dfw' => false,
self::$this_tinymce = ( $set['tinymce'] && user_can_richedit() );
if ( self::$this_tinymce ) {
if ( false !== strpos( $editor_id, '[' ) ) {
self::$this_tinymce = false;
_deprecated_argument( 'wp_editor()', '3.9.0', 'TinyMCE editor IDs cannot have brackets.' );
self::$this_quicktags = (bool) $set['quicktags'];
if ( self::$this_tinymce ) {
self::$has_tinymce = true;
if ( self::$this_quicktags ) {
self::$has_quicktags = true;
if ( empty( $set['editor_height'] ) ) {
if ( 'content' === $editor_id && empty( $set['tinymce']['wp_autoresize_on'] ) ) {
// A cookie (set when a user resizes the editor) overrides the height.
$cookie = (int) get_user_setting( 'ed_size' );
$set['editor_height'] = $cookie;
if ( $set['editor_height'] < 50 ) {
$set['editor_height'] = 50;
} elseif ( $set['editor_height'] > 5000 ) {
$set['editor_height'] = 5000;
* Outputs the HTML for a single instance of the editor.
* @param string $content Initial content for the editor.
* @param string $editor_id HTML ID for the textarea and TinyMCE and Quicktags instances.
* Should not contain square brackets.
* @param array $settings See _WP_Editors::parse_settings() for description.
public static function editor( $content, $editor_id, $settings = array() ) {
$set = self::parse_settings( $editor_id, $settings );
$editor_class = ' class="' . trim( esc_attr( $set['editor_class'] ) . ' wp-editor-area' ) . '"';
$tabindex = $set['tabindex'] ? ' tabindex="' . (int) $set['tabindex'] . '"' : '';
$default_editor = 'html';
$editor_id_attr = esc_attr( $editor_id );
if ( $set['drag_drop_upload'] ) {
self::$drag_drop_upload = true;
if ( ! empty( $set['editor_height'] ) ) {
$height = ' style="height: ' . (int) $set['editor_height'] . 'px"';
$height = ' rows="' . (int) $set['textarea_rows'] . '"';
if ( ! current_user_can( 'upload_files' ) ) {
$set['media_buttons'] = false;
if ( self::$this_tinymce ) {
$autocomplete = ' autocomplete="off"';
if ( self::$this_quicktags ) {
$default_editor = $set['default_editor'] ? $set['default_editor'] : wp_default_editor();
// 'html' is used for the "Text" editor tab.
if ( 'html' !== $default_editor ) {
$default_editor = 'tinymce';
$buttons .= '<button type="button" id="' . $editor_id_attr . '-tmce" class="wp-switch-editor switch-tmce"' .
' data-wp-editor-id="' . $editor_id_attr . '">' . _x( 'Visual', 'Name for the Visual editor tab' ) . "</button>\n";
$buttons .= '<button type="button" id="' . $editor_id_attr . '-html" class="wp-switch-editor switch-html"' .
' data-wp-editor-id="' . $editor_id_attr . '">' . _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ) . "</button>\n";
$default_editor = 'tinymce';
$switch_class = 'html' === $default_editor ? 'html-active' : 'tmce-active';
$wrap_class = 'wp-core-ui wp-editor-wrap ' . $switch_class;
if ( $set['_content_editor_dfw'] ) {
$wrap_class .= ' has-dfw';
echo '<div id="wp-' . $editor_id_attr . '-wrap" class="' . $wrap_class . '">';
if ( self::$editor_buttons_css ) {
wp_print_styles( 'editor-buttons' );
self::$editor_buttons_css = false;
if ( ! empty( $set['editor_css'] ) ) {
echo $set['editor_css'] . "\n";
if ( ! empty( $buttons ) || $set['media_buttons'] ) {
echo '<div id="wp-' . $editor_id_attr . '-editor-tools" class="wp-editor-tools hide-if-no-js">';
if ( $set['media_buttons'] ) {
self::$has_medialib = true;
if ( ! function_exists( 'media_buttons' ) ) {
require ABSPATH . 'wp-admin/includes/media.php';
echo '<div id="wp-' . $editor_id_attr . '-media-buttons" class="wp-media-buttons">';
* Fires after the default media button(s) are displayed.
* @param string $editor_id Unique editor identifier, e.g. 'content'.
do_action( 'media_buttons', $editor_id );
echo '<div class="wp-editor-tabs">' . $buttons . "</div>\n";
if ( self::$this_quicktags ) {
if ( 'content' === $editor_id && ! empty( $GLOBALS['current_screen'] ) && 'post' === $GLOBALS['current_screen']->base ) {
$toolbar_id = 'ed_toolbar';
$toolbar_id = 'qt_' . $editor_id_attr . '_toolbar';
$quicktags_toolbar = '<div id="' . $toolbar_id . '" class="quicktags-toolbar"></div>';
* Filters the HTML markup output that displays the editor.
* @param string $output Editor's HTML markup.
$the_editor = apply_filters(
'<div id="wp-' . $editor_id_attr . '-editor-container" class="wp-editor-container">' .
'<textarea' . $editor_class . $height . $tabindex . $autocomplete . ' cols="40" name="' . esc_attr( $set['textarea_name'] ) . '" ' .
'id="' . $editor_id_attr . '">%s</textarea></div>'
// Prepare the content for the Visual or Text editor, only when TinyMCE is used (back-compat).
if ( self::$this_tinymce ) {
add_filter( 'the_editor_content', 'format_for_editor', 10, 2 );
* Filters the default editor content.
* @param string $content Default editor content.
* @param string $default_editor The default editor for the current user.
* Either 'html' or 'tinymce'.
$content = apply_filters( 'the_editor_content', $content, $default_editor );
// Remove the filter as the next editor on the same page may not need it.
if ( self::$this_tinymce ) {
remove_filter( 'the_editor_content', 'format_for_editor' );
// Back-compat for the `htmledit_pre` and `richedit_pre` filters.
if ( 'html' === $default_editor && has_filter( 'htmledit_pre' ) ) {
/** This filter is documented in wp-includes/deprecated.php */
$content = apply_filters_deprecated( 'htmledit_pre', array( $content ), '4.3.0', 'format_for_editor' );
} elseif ( 'tinymce' === $default_editor && has_filter( 'richedit_pre' ) ) {
/** This filter is documented in wp-includes/deprecated.php */
$content = apply_filters_deprecated( 'richedit_pre', array( $content ), '4.3.0', 'format_for_editor' );
if ( false !== stripos( $content, 'textarea' ) ) {
$content = preg_replace( '%</textarea%i', '</textarea', $content );
printf( $the_editor, $content );
self::editor_settings( $editor_id, $set );
* @param string $editor_id Unique editor identifier, e.g. 'content'.
* @param array $set Array of editor arguments.
public static function editor_settings( $editor_id, $set ) {
if ( empty( self::$first_init ) ) {
add_action( 'admin_print_footer_scripts', array( __CLASS__, 'editor_js' ), 50 );
add_action( 'admin_print_footer_scripts', array( __CLASS__, 'force_uncompressed_tinymce' ), 1 );
add_action( 'admin_print_footer_scripts', array( __CLASS__, 'enqueue_scripts' ), 1 );
add_action( 'wp_print_footer_scripts', array( __CLASS__, 'editor_js' ), 50 );
add_action( 'wp_print_footer_scripts', array( __CLASS__, 'force_uncompressed_tinymce' ), 1 );
add_action( 'wp_print_footer_scripts', array( __CLASS__, 'enqueue_scripts' ), 1 );
if ( self::$this_quicktags ) {
if ( is_array( $set['quicktags'] ) ) {
$qtInit = array_merge( $qtInit, $set['quicktags'] );
if ( empty( $qtInit['buttons'] ) ) {
$qtInit['buttons'] = 'strong,em,link,block,del,ins,img,ul,ol,li,code,more,close';
if ( $set['_content_editor_dfw'] ) {
$qtInit['buttons'] .= ',dfw';
* Filters the Quicktags settings.
* @param array $qtInit Quicktags settings.
* @param string $editor_id Unique editor identifier, e.g. 'content'.
$qtInit = apply_filters( 'quicktags_settings', $qtInit, $editor_id );
self::$qt_settings[ $editor_id ] = $qtInit;
self::$qt_buttons = array_merge( self::$qt_buttons, explode( ',', $qtInit['buttons'] ) );
if ( self::$this_tinymce ) {
if ( empty( self::$first_init ) ) {
$baseurl = self::get_baseurl();
$mce_locale = self::get_mce_locale();
* Filters the list of teenyMCE plugins.
* @since 3.3.0 The `$editor_id` parameter was added.
* @param array $plugins An array of teenyMCE plugins.
* @param string $editor_id Unique editor identifier, e.g. 'content'.
$plugins = apply_filters(
* Filters the list of TinyMCE external plugins.
* The filter takes an associative array of external plugins for
* TinyMCE in the form 'plugin_name' => 'url'.
* The url should be absolute, and should include the js filename
* to be loaded. For example:
* 'myplugin' => 'http://mysite.com/wp-content/plugins/myfolder/mce_plugin.js'.
* If the external plugin adds a button, it should be added with
* one of the 'mce_buttons' filters.
* @since 5.3.0 The `$editor_id` parameter was added.
* @param array $external_plugins An array of external TinyMCE plugins.
* @param string $editor_id Unique editor identifier, e.g. 'content'. Accepts 'classic-block'
* when called from block editor's Classic block.
$mce_external_plugins = apply_filters( 'mce_external_plugins', array(), $editor_id );
if ( ! self::$has_medialib ) {
* Filters the list of default TinyMCE plugins.
* The filter specifies which of the default plugins included
* in WordPress should be added to the TinyMCE instance.
* @since 5.3.0 The `$editor_id` parameter was added.
* @param array $plugins An array of default TinyMCE plugins.
* @param string $editor_id Unique editor identifier, e.g. 'content'. Accepts 'classic-block'
* when called from block editor's Classic block.
$plugins = array_unique( apply_filters( 'tiny_mce_plugins', $plugins, $editor_id ) );
$key = array_search( 'spellchecker', $plugins, true );
// Remove 'spellchecker' from the internal plugins if added with 'tiny_mce_plugins' filter to prevent errors.
// It can be added with 'mce_external_plugins'.
unset( $plugins[ $key ] );
if ( ! empty( $mce_external_plugins ) ) {
* Filters the translations loaded for external TinyMCE 3.x plugins.
* The filter takes an associative array ('plugin_name' => 'path')
* where 'path' is the include path to the file.
* The language file should follow the same format as wp_mce_translation(),
* and should define a variable ($strings) that holds all translated strings.
* @since 5.3.0 The `$editor_id` parameter was added.
* @param array $translations Translations for external TinyMCE plugins.
* @param string $editor_id Unique editor identifier, e.g. 'content'.
$mce_external_languages = apply_filters( 'mce_external_languages', array(), $editor_id );
if ( ! empty( $mce_external_languages ) ) {
foreach ( $mce_external_languages as $name => $path ) {
if ( @is_file( $path ) && @is_readable( $path ) ) {
$ext_plugins .= $strings . "\n";
foreach ( $mce_external_plugins as $name => $url ) {
if ( in_array( $name, $plugins, true ) ) {
unset( $mce_external_plugins[ $name ] );