5

How can I disable automatic paragraph tags, but retain line breaks?

I have used this to remove wpautop:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

But there are no more line breaks in the text.

  • 2
    Could you elaborate on what exactly you mean? You want <br /> tags, I assume? (EOLs should be there) – Johannes Pille May 8 '13 at 22:14
  • Yes, I want br tags. Do you mean a simple nl2br function should do it? – Pier May 8 '13 at 23:03
  • Yes, a simple nl2br function did it! – Pier May 8 '13 at 23:14
  • @Pier Add a real answer with your working code please. Accept that answer and get some rep. :) – fuxia May 9 '13 at 4:43
4

Here's the full solution.

First disable wpautop in your functions.php

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

Then parse your content/excerpt with nl2br (a standard PHP function).

add_filter( 'the_content', 'nl2br' );
add_filter( 'the_excerpt', 'nl2br' );
  • That will work, but it's very rudimentary, so you'll need to be look out for unintended side effects. For example, shortcodes will probably no longer work. – Simon Blackbourn Apr 30 '14 at 23:14
  • I wouldn't make this a filter, for that reason, but I would put it in single-post.php as echo nl2br(do_shortcode($post->post_content), true); Otherwise, allowing wpautop creates a whole host of other problems with your html in posts. – kloddant Nov 8 '18 at 15:19

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.