4

I often add   between connecting words so they break down together to a new line in important text. Switching to visual tab then back to text tab removes these. What can I do to keep them and avoid TinyMCE removing them?

I found this but not sure how to apply it to WordPress: https://www.abeautifulsite.net/tinymce-removes-non-breaking-spaces

My ugly solutions would be using a different custom string between words__like__this then later replacing them to   in PHP, before display. Or just typing it on the visual tab, which translates to   but then converting them with PHP.

6

This seems to do it:

function allow_nbsp_in_tinymce( $mceInit ) {
    $mceInit['entities'] = '160,nbsp,38,amp,60,lt,62,gt';   
    $mceInit['entity_encoding'] = 'named';
    return $mceInit;
}
add_filter( 'tiny_mce_before_init', 'allow_nbsp_in_tinymce' );

Also see: https://www.tinymce.com/docs/configure/content-filtering/#entities

Any improvement suggestions?

  • 2
    Small improvement: you don’t have to repeat the existing entities (amp, lt, gt) in the config: $mceInit['entities'] .= ',160,nbsp'; – Geert Sep 3 '16 at 14:42
  • 1
    By the way, many may find it useful to also preserve soft hyphens. Just add 173,shy to the entities list. – Geert Sep 3 '16 at 14:54
  • Is there is a documentation on add_filter() or something? How to implement this in TinyMCE 4.x? – Alph.Dev Nov 15 '17 at 20:44
  • @Alph.Dev add_filter() is a wordpress function : developer.wordpress.org/reference/functions/add_filter – AyexeM Mar 14 '19 at 14:03

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.