6

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.

  • 2
    I got to the point where I was having clients paste into notepad/textedit first, hope you find a better solution. – Andrew Bartel Nov 27 '14 at 17:50
  • @AndrewBartel Absolutely, this has by far been our biggest obstacle. I believe we struggled with Word for a full year until we found a complete solution, just to see that the issue is still present with other apps. – Christine Cooper Nov 27 '14 at 18:11
  • Have you looked at the default TinyMCE Plugins on GitHub such as there are quite some examples for paste. – kaiser Nov 27 '14 at 19:12
  • I think we need something along the lines of the paste_word_valid_elements init option but for generic paste events. – Christine Cooper Nov 28 '14 at 16:39
  • What formatting is getting through? I tried pasting various bold/underline/font/table/list stuff using TextEdit (and a dubious copy of Pages) and it was stripped ok. – bonger Dec 13 '14 at 6:28
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.