Remove empty lines ( ) when author updates their post

Whenever adding an empty line between paragraphs using TinyMCE, the   character entity is added.

How can I strip the content of all instances of this character whenever an author updates their post (save_post)?

Related posts

Leave a Reply

2 comments

  1. Figured it out, hooking into content_save_pre:

    function remove_empty_lines( $content ){
    
      // replace empty lines
      $content = preg_replace("/ /", "", $content);
    
      return $content;
    }
    add_action('content_save_pre', 'remove_empty_lines');
    
  2. I liked your solution but it might happen to be that the ” ” is legit or intended in some part of the content further down in the content structure. The problem – at least for me – only occurs for the unnecessary and annoying extra lines at the beginning of the content.
    So I decided to extend your solution by only removing the extra “nonBreakingSpaces” at the very beginning of the text before any further tags occur:

    function remove_empty_lines( $content ){
    
      // replace empty lines
    $contentArr = explode('<',$content,2);
    if (count($contentArr)==2) // only then  
    { 
        $contentArr[0] = preg_replace("/&nbsp;/", "", $contentArr[0]);
        $content = $contentArr[0].'<'.$contentArr[1]; 
    }  
    return $content;
    }
    add_action('content_save_pre', 'remove_empty_lines');