2

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)?

7

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');
1

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');

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.