Super Cleansing Cut/Paste into Tiny MCE in WordPress

Had someone cut/pasting from Google Docs into WordPress and it was ending up littered with internal style elements and super irritating span classes. I found this post which got me 95% of the way there and just added .removeAttr(‘style’) to get rid of the inline styles.

Now I can just re-cut/paste and get a clean chunk of text.


//fix cut paste drama from https://jonathannicol.com/blog/2015/02/19/clean-pasted-text-in-wordpress/
add_filter('tiny_mce_before_init','configure_tinymce');

/**
* Customize TinyMCE's configuration
*
* @param array
* @return array
*/
function configure_tinymce($in) {
$in['paste_preprocess'] = "function(plugin, args){
// Strip all HTML tags except those we have whitelisted
var whitelist = 'p,b,strong,i,em,h2,h3,h4,h5,h6,ul,li,ol,a,href';
var stripped = jQuery('<div>' + args.content + '</div>');
var els = stripped.find('*').not(whitelist);
for (var i = els.length - 1; i >= 0; i--) {
var e = els[i];
jQuery(e).replaceWith(e.innerHTML);
}
// Strip all class and id attributes
stripped.find('*').removeAttr('id').removeAttr('class').removeAttr('style');
// Return the clean HTML
args.content = stripped.html();
}";
return $in;
}