Thursday, 27 July 2006
Restoring backslashes in WordPress
Sorry if anyone tried the scripts I posted yesterday and found them inoperative—they were missing backslashes. I used to always verify this stuff back when I was using more finicky blogging tools, but given WordPress generally does the right thing™ I didn’t worry.
WordPress strips backslashes inside <pre> tags because quotes get escaped. I spent a while searching for the responsible regular expression substitution before finding out PHP had a function to strip backslashes. The line responsible is the second last of wpautop in wp-includes/functions-formatting.php:
$pee = preg_replace('!(</pre><pre .*?=".*?">)(.*?)</pre>!ise', " stripslashes('$1')
. stripslashes(clean_pre('$2')) . '' ", $pee);
While this eliminates the extra backslashes before quotation marks, it also removes other backslashes. Here’s a fix:
$pee = preg_replace('!(</pre><pre .*?=".*?">)(.*?)!ise', " stripslashes('$1')
. str_replace('\\\\\"', '\"', clean_pre('$2')) . '' ", $pee);
2:57 PM |