1

I removed wpautop from my theme:

function disable_linebreaks($content) {
       remove_filter ('the_content','wpautop');
       return $content;
}
add_filter('the_content','disable_linebreaks',1);

But now, when switching between HTML and Editor ALL p-tags disappear, even the ones I coded myself.

What I want is just to prevent Wordpress from adding empty <p></p> - Code. Basically I'm happy with wpautop, it's just that sometimes it's too much (For example around images. There's always an empty p before every image).

4
  • Do you intend to use or not use wpautop?
    – s_ha_dum
    Jul 29, 2013 at 14:19
  • I dont want to use it. I just want to remove those empty p-tags. I thought disabling autop would do that, but what it does is really too much. Jul 30, 2013 at 11:15
  • If someone can tell me a way to remove autop and keep the p-tags, I wrote myself, that would be ok, too. Jul 30, 2013 at 11:18
  • possible duplicate of remove empty paragraphs from the_content? Jul 30, 2013 at 11:37

1 Answer 1

2

If what you want is really only

to prevent Wordpress from adding empty <p></p>

and you'd be happy with removing those after post retrieval from the database, then

function wpse108194_remove_empty_paragraphs( $content ) {
   $content = preg_replace( '#<p>\s*</p>#', '', $content );
   return $content;
}
add_filter( 'the_content', 'wpse108194_remove_empty_paragraphs', 11 );

will do.

7
  • I already tried this yesterday. For me it doesn't work (They are still there). Im using the roots theme, it has a lot of functions already built in. Maybe there is a conflict, I don't know. I read, that it does not work for other people, too. Jul 30, 2013 at 11:17
  • What does not work for other people? I wrote the above yesterday. That being said, I copied your above function and altered the code - it might be that 1 is too low a priority and that wpautop runs thereafter. Change it to a higher number (as I have now done in the answer) to make it run later. Jul 30, 2013 at 11:23
  • preg_replace does not work. I tried it with priority 9999, too. Nothing. There is a similar question, where somebody said, that this did not work. But after that, there where no further responses. Jul 30, 2013 at 11:25
  • I will try it again, this evening, with exactly your code. But I dont think, it will work, as I tried it in a few different ways. Jul 30, 2013 at 11:26
  • 1
    I did find the question you are referring to. And from reading that it appears the above might indeed not be a solution. Why that'd be the case is beyond me - it should work, in theory. Now we face another issue though: Your question is an exact duplicate of an existing one. I don't have the time for thorough investigation right now, but this is a topic that should have a concrete answer on this site. Might look into it later. Would answer in the existing one, though. This should be closed. Jul 30, 2013 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.