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.

1 Answer 1

7

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?

5
  • 3
    Small improvement: you don’t have to repeat the existing entities (amp, lt, gt) in the config: $mceInit['entities'] .= ',160,nbsp';
    – Geert
    Sep 3, 2016 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, 2016 at 14:54
  • Is there is a documentation on add_filter() or something? How to implement this in TinyMCE 4.x?
    – Slava
    Nov 15, 2017 at 20:44
  • @Alph.Dev add_filter() is a wordpress function : developer.wordpress.org/reference/functions/add_filter Mar 14, 2019 at 14:03
  • Does this will work with elementor Jun 24, 2020 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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