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
)?
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
)?
2 Answers
Reset to default 7Figured 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');
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("/ /", "", $contentArr[0]);
$content = $contentArr[0].'<'.$contentArr[1];
}
return $content;
}
add_action('content_save_pre', 'remove_empty_lines');