Possible to stop WordPress from adding p1, p2… classes to p tags in TinyMCE?

I figured out how to strip those classes using TinyMCE’s paste_preprocess option.

In your functions.php:

add_filter('tiny_mce_before_init', 'customize_tinymce');

function customize_tinymce($in) {
  $in['paste_preprocess'] = "function(pl,o){ o.content = o.content.replace(/p class=\"p[0-9]+\"/g,'p'); o.content = o.content.replace(/span class=\"s[0-9]+\"/g,'span'); }";
  return $in;
}

The value passed to paste_preprocess is a JavaScript function which will be executed whenever content is pasted into TinyMCE. The function uses a regular expression to strip instances of e.g. class="p1" and class="s1" on p and span tags.

Note that this is for WordPress 4.x, which uses TinyMCE 4.

I’d still love to know where those classes originate. It’s odd that multiple applications append exactly the same classes to HTML tags…

Leave a Comment