Support » Plugins » Hacks » Prevent &nbsp characters from getting removed by the visual editor

  • Resolved Pt


    I’m a graphic designer and want my   (non-breaking space) characters to stay where I bloody-well put them without the visual editor stripping them out again. If you’re a typography dick like me, this is handy to make two or more specific words wrap together rather than breaking at the spaces.

    For example, if you want “South Australia” not to break in an address.

    So, I wrote added a simple shortcode via my child-theme’s functions.php:

    //[nbsp] shortcode
    function nbsp_shortcode( $atts, $content = null ) {
    	$content = '&nbsp';
    	return $content;
    add_shortcode( 'nbsp', 'nbsp_shortcode' );

    Then I just use:
    Instead of:

    Hope this helps someone.


Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz


    Great tip! Thanks for sharing. The stripping of whitespace is one of the many reasons I hate the built in editor.

    Nice! Thanks!

    Nice solution. In your example, it may be better to write <span class="nowrap">South Australia</span> with the style .nowrap{white-space: nowrap;}.

    But there are situations where preserving nbsp is necessary.

    Very nice. Saved me. Thank you.

    Hey Pt,

    Beautiful and excellent shortcode!

    I wander though. Is it possible for you to modify it so you can specify the number of returned &nbsp?

    For example: [nbsp num_nbsp=”5″]

    Really appreciate it and thank you in advance.

    Best regards,

    Hi Fotiou,

    try the following:

    //[nbsp] shortcode - Create a non-breaking space where I bloody-well want one.
    function nbsp_shortcode( $atts, $content = null ) {
    	//Set default $content
    	$content = '&nbsp';
    	//Set default count. This will be overriden if a count="" value is specified.
    	$a = shortcode_atts( array(
            'count' => '1',
        ), $atts );
    	//Set the count variable from the shortcode_atts array.
        $count = $a['count'];
        //Make it an integer value.
        $count = intval($count);
    	//Check if $count is an integer value and is greater than 0. If so, string-repeat $content X $count.
    	if (is_int($count) && $count >= 1) {
    		$content = str_repeat($content, $count);
    	return $content;
    add_shortcode( 'nbsp', 'nbsp_shortcode' );

    Use as either:
    [nbsp] for one space or [nbsp count="5"] for 5 spaces (or however many you want).

    Note: I did this in something of a long form way for self-education. Someone with better PHP-Fu could undoubtedly make it much shorter and more elegant.

    Also note: there are better ways to add space than multiple &nbsp;s but I can imagine how this could be useful.

    Dear Pt,

    Thank you for your prompt response!

    I shall try it in the day and I will came back to you…

    Again, thank you for your kind help.

    Hey pt,

    Tested your code and is working perfectly for what I need to do!

    Again, thanks a lot!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Prevent &nbsp characters from getting removed by the visual editor’ is closed to new replies.