Why does Wordpress 3.0 add <p> tags around my comments?

I was just creating a new WordPress 3.0 site and noticed that my formatting was all screwed up because WordPress was adding <p></p> tags around my comments—I like to write well-formatted code, so this was driving me crazy. I was completely stumped because by default I disable the wpautop function in all installations of WordPress—there are plenty of sites which tell you how to do this in older versions of WordPress, so I won't get into that here. I did some research and I found out that in WordPress 3.0 there have been changes that prevent the old fix from working with regard to comments.

I found a post made by Azmeen of HTNet who provided the following lines to fix this:

$pee = str_replace("<p><!--", "<!--", $pee);
$pee = str_replace("--></p>", "-->", $pee);

Open /wp-includes/formatting.php and search for return $pee; (about line 220). Add those lines above it.

That didn't entirely solve the problem for me, because I always put comments after my </div> tags. I was still ending up with an extra </p> before my closing divs. So I added the following additional line:

$pee = str_replace("</p>\n</p></div>","</p></div>", $pee);

That solved the problem—I get a line break right after the </p> and before the comment, but I can live with that.

Back to the main WordPress Q&A page