How to stop WordPress removing line breaks

Posted in: Blog- Feb 17, 2010 2 Comments

We’ve all had these problems working with WordPress as designers where you are trying to format an article or something for a client and have everything lined up perfectly, publish the article, only to find out that WordPress has removed all your manual line-breaks!

Fortunately there is a pretty simple fix, I had to edit formatting.php in order to keep wpautop() from removing my deliberate breaks. I commented out the following line which replaces two occurrences of BR separated only by whitespace with two newlines:

//$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);

Then, I commented out this line found towards the end of the function and replaced it with a version that exempts BRs from being removed:

Commented out:
//$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);

Replaced with:
$pee = preg_replace('!(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);

This keeps wordpress from stripping the breaks, but it does not keep Tiny MCE from doing it if you go to visual mode.

2 Responses to “How to stop WordPress removing line breaks”

  1. Flash Buddy says:

    How does this hold up upon upgrading versions?

  2. Shaan Nicol says:

    unfortunately you will have to make this change every time you upgrade wordpress

Leave a Reply