Completely strip any hidden formatting when pasting into TinyMCE

In WordPress v4+, I want to remove all hidden formatting when users paste content into TinyMCE visual editor.

The Paste as Text button works when users insert text from Microsoft Word but doesn’t do its job with other applications like Pages for OSX.

You can use the following to filter away all formatting for Word (thanks Till Kruss):

class PasteAsPlainText {

    function __construct() {

        add_action( 'admin_init', array( $this, 'init' ) );

    }

    function init() {

        add_filter( 'tiny_mce_before_init', array( $this, 'forcePasteAsPlainText' ) );
        add_filter( 'teeny_mce_before_init', array( $this, 'forcePasteAsPlainText' ) );
        add_filter( 'teeny_mce_plugins', array( $this, 'loadPasteInTeeny' ) );
        add_filter( 'mce_buttons_2', array( $this, 'removePasteAsPlainTextButton' ) );

    }

    function forcePasteAsPlainText( $mceInit ) {

        global $tinymce_version;

        if ( $tinymce_version[0] < 4 ) {
            $mceInit[ 'paste_text_sticky' ] = true;
            $mceInit[ 'paste_text_sticky_default' ] = true;
        } else {
            $mceInit[ 'paste_as_text' ] = true;
        }

        return $mceInit;
    }

    function loadPasteInTeeny( $plugins ) {

        return array_merge( $plugins, (array) 'paste' );

    }

    function removePasteAsPlainTextButton( $buttons ) {

        if( ( $key = array_search( 'pastetext', $buttons ) ) !== false ) {
            unset( $buttons[ $key ] );
        }

        return $buttons;

    }

}

new PasteAsPlainText();

And then you can hide the Paste as Text button (to disallow users from unticking it) by selecting which buttons you want to show:

function formatTinyMCE( $in ) {
    $in['toolbar1'] = 'bold,custom_em,blockquote,aligncenter,link,unlink,spellchecker,undo,removeformat';
    return $in; 
}
add_filter( 'tiny_mce_before_init', 'formatTinyMCE' );

Now that we have got Word out of the way (finally), how do we completely strip all hidden formatting pasted into TinyMCE?

UPDATE: One approach could be to find an init option like paste_word_valid_elements and have an empty list of valid tags.

1
1

I’m not quite understand what you need but I think that you trying to insert text as plain text from anywhere: Browser, M$ Word, Pages.

For this purpose you can use Advanced TinyMCE Configuration plugin. After download and activating this plugin requires manual configuration. This solution better than code you suggested in question section above).

Option name                  Value
paste_strip_class_attributes all
paste_remove_spans           true
paste_remove_styles          true
paste_as_text                true
paste_text_sticky            true
block_formats                Paragraph=p;Header 2=h2;...

All of these parts (parameters) of config may be founded on Tiny MCE documentation pages.

  1. paste_strip_class_attributes — old attribute from 3.?.? version
    for removing all tags attributes like href, alt, class,
    onError,..
  2. paste_remove_spans — also from 3.?.? version. Enables you to
    remove all span (inline?) elements while pasting.
  3. paste_remove_styles — If true, removes all style information when
    pasting, regardless of browser type. Pasting from Word 2000 will
    cause tinyMCE to error.
  4. paste_as_text — This option enables you to set the default state
    of the “paste as text” edit menu option. It’s disabled by default
    but can be forced on by default.
  5. paste_text_sticky — Keeps Paste Text feature active until user
    deselects the Paste as Text button.
  6. block_formats — additional configuration for my site 🙂

This config working fine on my sites and I always insert text in TinyMCE like a plain text.

Also helpfull plugin for TinyMCE tuning TinyMCE Advanced.

Leave a Comment