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.

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

1 Answer 1

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' ); 
2
  • 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. Commented Apr 30, 2014 at 23:14
  • 1
    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. Commented Nov 8, 2018 at 15:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.