: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Similar to `wpautop()` in formatting.php.
* @memberof switchEditors
* @param {string} text The text input.
* @return {string} The formatted text.
var preserve_linebreaks = false,
blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' +
'|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' +
'|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary';
// Normalize line breaks.
text = text.replace( /\r\n|\r/g, '\n' );
// Remove line breaks from <object>.
if ( text.indexOf( '<object' ) !== -1 ) {
text = text.replace( /<object[\s\S]+?<\/object>/g, function( a ) {
return a.replace( /\n+/g, '' );
// Remove line breaks from tags.
text = text.replace( /<[^<>]+>/g, function( a ) {
return a.replace( /[\n\t ]+/g, ' ' );
// Preserve line breaks in <pre> and <script> tags.
if ( text.indexOf( '<pre' ) !== -1 || text.indexOf( '<script' ) !== -1 ) {
preserve_linebreaks = true;
text = text.replace( /<(pre|script)[^>]*>[\s\S]*?<\/\1>/g, function( a ) {
return a.replace( /\n/g, '<wp-line-break>' );
if ( text.indexOf( '<figcaption' ) !== -1 ) {
text = text.replace( /\s*(<figcaption[^>]*>)/g, '$1' );
text = text.replace( /<\/figcaption>\s*/g, '</figcaption>' );
// Keep <br> tags inside captions.
if ( text.indexOf( '[caption' ) !== -1 ) {
text = text.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) {
a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' );
a = a.replace( /<[^<>]+>/g, function( b ) {
return b.replace( /[\n\t ]+/, ' ' );
return a.replace( /\s*\n\s*/g, '<wp-temp-br />' );
text = text.replace( /<br \/>\s*<br \/>/gi, '\n\n' );
// Pad block tags with two line breaks.
text = text.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n\n$1' );
text = text.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' );
text = text.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' );
// Remove white space chars around <option>.
text = text.replace( /\s*<option/gi, '<option' );
text = text.replace( /<\/option>\s*/gi, '</option>' );
// Normalize multiple line breaks and white space chars.
text = text.replace( /\n\s*\n+/g, '\n\n' );
// Convert two line breaks to a paragraph.
text = text.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' );
// Remove empty paragraphs.
text = text.replace( /<p>\s*?<\/p>/gi, '');
// Remove <p> tags that are around block tags.
text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
text = text.replace( /<p>(<li.+?)<\/p>/gi, '$1');
// Fix <p> in blockquotes.
text = text.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');
text = text.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');
// Remove <p> tags that are wrapped around block tags.
text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
text = text.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
text = text.replace( /(<br[^>]*>)\s*\n/gi, '$1' );
text = text.replace( /\s*\n/g, '<br />\n');
// Remove <br> tags that are around block tags.
text = text.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
text = text.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
// Remove <p> and <br> around captions.
text = text.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' );
// Make sure there is <p> when there is </p> inside block tags that can contain other blocks.
text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) {
if ( c.match( /<p( [^>]*)?>/ ) ) {
return b + '<p>' + c + '</p>';
// Restore the line breaks in <pre> and <script> tags.
if ( preserve_linebreaks ) {
text = text.replace( /<wp-line-break>/g, '\n' );
// Restore the <br> tags in captions.
text = text.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' );
* Fires custom jQuery events `beforePreWpautop` and `afterPreWpautop` when jQuery is available.
* @memberof switchEditors
* @param {string} html The content from the visual editor.
* @return {string} the filtered content.
function pre_wpautop( html ) {
var obj = { o: exports, data: html, unfiltered: html };
$( 'body' ).trigger( 'beforePreWpautop', [ obj ] );
obj.data = removep( obj.data );
$( 'body' ).trigger( 'afterPreWpautop', [ obj ] );
* Fires custom jQuery events `beforeWpautop` and `afterWpautop` when jQuery is available.
* @memberof switchEditors
* @param {string} text The content from the text editor.
* @return {string} filtered content.
function wpautop( text ) {
var obj = { o: exports, data: text, unfiltered: text };
$( 'body' ).trigger( 'beforeWpautop', [ obj ] );
obj.data = autop( obj.data );
$( 'body' ).trigger( 'afterWpautop', [ obj ] );
} else if ( document.addEventListener ) {
document.addEventListener( 'DOMContentLoaded', init, false );
window.addEventListener( 'load', init, false );
} else if ( window.attachEvent ) {
window.attachEvent( 'onload', init );
document.attachEvent( 'onreadystatechange', function() {
if ( 'complete' === document.readyState ) {
wp.editor.autop = wpautop;
wp.editor.removep = pre_wpautop;
pre_wpautop: pre_wpautop,
* Expose the switch editors to be used globally.
* @namespace switchEditors
window.switchEditors = new SwitchEditors();
* Initialize TinyMCE and/or Quicktags. For use with wp_enqueue_editor() (PHP).
* Intended for use with an existing textarea that will become the Text editor tab.
* The editor width will be the width of the textarea container, height will be adjustable.
* Settings for both TinyMCE and Quicktags can be passed on initialization, and are "filtered"
* with custom jQuery events on the document element, wp-before-tinymce-init and wp-before-quicktags-init.
* @param {string} id The HTML id of the textarea that is used for the editor.
* Has to be jQuery compliant. No brackets, special chars, etc.
* @param {Object} settings Example:
* // See https://www.tinymce.com/docs/configure/integration-and-setup/.
* // Alternatively set to `true` to use the defaults.
* setup: function( editor ) {
* console.log( 'Editor initialized', editor );
* // Alternatively set to `true` to use the defaults.
* buttons: 'strong,em,link'
wp.editor.initialize = function( id, settings ) {
if ( ! $ || ! id || ! wp.editor.getDefaultSettings ) {
defaults = wp.editor.getDefaultSettings();
// Initialize TinyMCE by default.
// Add wrap and the Visual|Text tabs.
if ( settings.tinymce && settings.quicktags ) {
var $textarea = $( '#' + id );
var $wrap = $( '<div>' ).attr( {
'class': 'wp-core-ui wp-editor-wrap tmce-active',
var $editorContainer = $( '<div class="wp-editor-container">' );
var $button = $( '<button>' ).attr( {
var $editorTools = $( '<div class="wp-editor-tools">' );
if ( settings.mediaButtons ) {
var buttonText = 'Add Media';
if ( window._wpMediaViewsL10n && window._wpMediaViewsL10n.addMedia ) {
buttonText = window._wpMediaViewsL10n.addMedia;
var $addMediaButton = $( '<button type="button" class="button insert-media add_media">' );
$addMediaButton.append( '<span class="wp-media-buttons-icon"></span>' );
$addMediaButton.append( document.createTextNode( ' ' + buttonText ) );
$addMediaButton.data( 'editor', id );
$( '<div class="wp-media-buttons">' )
.append( $addMediaButton )
.append( $( '<div class="wp-editor-tabs">' )
.append( $button.clone().attr({
'class': 'wp-switch-editor switch-tmce'
}).text( window.tinymce.translate( 'Visual' ) ) )
'class': 'wp-switch-editor switch-html'
}).text( window.tinymce.translate( 'Text' ) ) )
).append( $editorContainer )
$textarea.after( $wrap );
$editorContainer.append( $textarea );
if ( window.tinymce && settings.tinymce ) {
if ( typeof settings.tinymce !== 'object' ) {
init = $.extend( {}, defaults.tinymce, settings.tinymce );
init.selector = '#' + id;
$( document ).trigger( 'wp-before-tinymce-init', init );
window.tinymce.init( init );
if ( ! window.wpActiveEditor ) {
window.wpActiveEditor = id;
if ( window.quicktags && settings.quicktags ) {
if ( typeof settings.quicktags !== 'object' ) {
init = $.extend( {}, defaults.quicktags, settings.quicktags );
$( document ).trigger( 'wp-before-quicktags-init', init );
window.quicktags( init );
if ( ! window.wpActiveEditor ) {
window.wpActiveEditor = init.id;
* Remove one editor instance.
* Intended for use with editors that were initialized with wp.editor.initialize().
* @param {string} id The HTML id of the editor textarea.
wp.editor.remove = function( id ) {
var mceInstance, qtInstance,
$wrap = $( '#wp-' + id + '-wrap' );
mceInstance = window.tinymce.get( id );
if ( ! mceInstance.isHidden() ) {
if ( window.quicktags ) {
qtInstance = window.QTags.getInstance( id );
$wrap.after( $( '#' + id ) );
* Get the editor content.
* Intended for use with editors that were initialized with wp.editor.initialize().
* @param {string} id The HTML id of the editor textarea.
* @return The editor content.
wp.editor.getContent = function( id ) {
editor = window.tinymce.get( id );
if ( editor && ! editor.isHidden() ) {
return $( '#' + id ).val();
}( window.jQuery, window.wp ));