When displaying a post, WordPress has the bad habbit of adding a few <br> tags. This often messes with the layout especially when you add images on the left or right side.
This is done by the wpautop filter. This function does not only add line breaks but also replaces double line breaks by HTML paragraphs i.e. <p>…</p> tags.
If you want to completely switch it off, you can just remove the filter by adding the following to your functions.php:
remove_filter('the_content','wpautop');
If you want to keep the paragraph tags but just make sure that no additional line breaks are inserted, you’ll need to replace the filter by your own. wpautop takes two parameters:
- $foo: The text to be formatted.
- $br: A boolean saying whether additional line breaks should be added.
So basically we just need to call the function with the text to be formatted and $br=0:
function wpautop_wo_br($foo){
return wpautop($foo,$br=0);
}
remove_filter('the_content','wpautop');
add_filter('the_content','wpautop_wo_br');
By adding this to your functions.php, you’ll get the desired effect. It’s that simple !
Thanks for the article.
Another issue is comment line break by default is single-line break. Is there a way to make it double line break?
I assume you “double line break” means starting a new paragraph here.
Basically it works the same as in the text post editor. If you have two newlines (i.e. an empty line inbetween), WordPress should create two paragraphs out of it.
The way it looks like then depends on the styles in you theme. Some theme tend to only add some padding below or around paragraphs which are in a post or page. If it is the case, you’ll have to extend it by adding some css.