Wordpress is creating a &nbsp in the_excerpt. How do I remove it?

<div class="subtitulo-noticia"><?php the_excerpt(); ?></div>

enter image description here

&nbsp creates a space at the beginning of p

enter image description here

Edit: Each space in the_content creates a &nbsp, this ends up creating a &nbsp in the_excerpt. How do I create spaces in content without creating &nbsp

  • looks to me like you need to edit the excerpt itself in the wp-admin, it looks like you have some spaces preceding the content.
    – RobBenz
    Jan 30, 2017 at 15:35
  • What exactly do I need to edit? Jan 30, 2017 at 16:00
  • the post itself. you need to edit the content typed into the post editor. and remove the leading spaces with the delete key
    – RobBenz
    Jan 30, 2017 at 19:38
  • maybe take a look here, if there are images in your post wordpress.stackexchange.com/questions/229679/…
    – RobBenz
    Jan 30, 2017 at 19:39
  • Each space in the content creates a &nbsp, but how do I make a space without creating it? Jan 30, 2017 at 22:04

6 Answers 6


I'm not sure why there's a non-breaking space being added to the front of your excerpt. It would be best to find out what is putting that there. But in the meantime, you can add this to your theme functions.php ( or a simple plugin ) and it should take care of it.

//* Remove non-breaking space from beginning of paragraph
add_filter( 'the_excerpt', function( $excerpt ) {
  return str_replace( [ '<p>&nbsp; ', '<p>&nbsp;' ], '<p>', $excerpt );
}, 999, 1 );
  • This just shows one ; In front of the text Jan 30, 2017 at 22:41
  • Oops. That was a typo. Sorry. Fixed it - notice the ; after the &nbsp now. Jan 31, 2017 at 0:38
  • Now it will remove the space after the non-breaking space as well if it's present. Jan 31, 2017 at 0:50

If you can't get rid of the extra space in the admin, then use trim()

<div class="subtitulo-noticia"><?php echo trim(get_the_excerpt()); ?></div>


  • Did not work, do I need to add something in functions.php? Jan 30, 2017 at 15:59
  • try trim(get_the_excerpt(), '&nbsp'); Really you should be able to remove that from the post/page editor. That extra space exists somewhere and is causing that &nbsp to show up.
    – mrben522
    Jan 30, 2017 at 16:24
  • Unfortunately it did not work either Jan 30, 2017 at 16:29
  • vardump get_the_excerpt() and see what it looks like.
    – mrben522
    Jan 30, 2017 at 17:05

Try with:

$text = str_replace( "&nbsp","",get_the_excerpt() );

Maybe, replacing &nbsp with an empty string could be fix your problem. The get_the_excerpt() function will get your post's excerpt and str_replace() substitute &nbsp.


This is my working solution:

<?php echo trim(preg_replace('/>\s+</m', '><', get_the_content()));?>

I used a modified version of Nathan's solution to resolve the issue of WordPress adding in extra empty heading and space tags. While not comprehensive, it does save me from editing 200+ pages in the CMS to bring them up to accessibility level compliance. Also, we know that even if we fix the 200+ pages someone will do it again, so it would become a reoccurring issue to resolve.

// Remove non-breaking space from beginning of paragraph
add_filter( 'the_content', function ( $content ) {
    $search = [

    return str_replace( $search, '', $content );
}, 999 );

Insert this code into your theme's functions.php file

add_filter( 'the_excerpt', function( $excerpt ) {
  return str_replace( '&nbsp;', '', $excerpt );
}, 999, 1);

Your Answer

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

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