WordPress replacing all line breaks in comments with “rn”

This concerns a problem at my blog.

My provider recently required me to upgrade from PHP 5.2 to PHP 5.4. This in turn required me to update WordPress.

Ever since the dual update, all line breaks in comments are rendering as “rn”, so that if you type this:

Hi
<br><br>
Hello

what gets rendered is

HirnHello

If I edit comments to repair them, all of the line breaks I enter are converted back to “rn” as soon as I hit “save”.

A temporary rollback to PHP 5.2 had no effect on this problem.

I briefly thought I could solve the problem by updating my theme (K2). This did not solve the problem and created a slew of new (unrelated) problems, so I’ve rolled back that update.

Browsing around this site, I see a few scattered references to this and similar problems, but no indication that it’s ever been common and nothing that appears to be a solution.

Can someone guess what’s causing this, and even better, how to fix it?

Update: I found a file called wp-comments-post.php, which contains the following line:

$comment_content = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;

I wrapped the trim($_POST['comment']) inside a nl2br(), and this helped a bit. Instead of rendering as

HirnHello

it now renders as

Hi<br>
rn<br>
rnHello

(or something like that; I might have forgotten how many rn‘s show up).

This is progress, but I’d still like to get rid of the rn‘s. Presumably I can use str_replace? But I am an amateur and am not sure of the syntax. My first few guesses didn’t work.

Meanwhile, I still want to know why this problem arose in the first place.

2 Answers
2

The above answer (“So it turns out”) turns out not to be the answer after all. With the fix described there, comments that I submitted were rendered correctly but comments that others submitted were not (even though I was submitting through the same comment interface that everyone else was). Go figure.

Anyway, the real fix turns out to be to restore wp-comments-post.php to its original form (eliminating the str_replace that I had added per the earlier answer) and to turn to a file called comments.php (which is part of the K2 theme), where the problem was solved by replacing the line

<?php comment_text(); ?>

with

<?php nl2br(comment_text());  ?>

This definitely works. Why it was suddenly necessary remains a great mystery.

Leave a Comment