Archives / Search ›

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);

3 comments on “Restoring backslashes in WordPress”

  1. 28 December 2011 | 8:05 AM

    The problem is still around, so thanks for the heads up the filename is now simply

    formatting.php

    28/12/2011

    Phil

  2. 28 December 2011 | 9:15 AM

    Depressing but good to know, thanks. (One of these days I need to upgrade this copy of WordPress…)

  3. 28 December 2011 | 10:19 AM

    A couple of things:

    You might like to see this post http://projecttz.com/?p=4469 how the backslash problem upset one of the worked examples for the Inline PHP plugin.

    If your happy with the version of WP you are using please stick with it. As I point out in the notes at the bottom of the post the personality of the software is changing into a much more complex beast in software and management terms.

    The complexity of the multi-layered relationships between all the different software is as you would expect taking its toll.

    Thanks for just about the only decent detailed solution on this problem floating around the binary sea :-)

    Phil

    *********************************************

    The following lines are supposed to show the number of literal backslash characters increasing by one on each line. Lets see what happens

    1 \
    2 \\
    3 \\\
    4 \\\\
    5 \\\\\
    6 \\\\\\
    7 \\\\\\\
    8 \\\\\\\\
    9 \\\\\\\\\
    10 \\\\\\\\\\
    11 \\\\\\\\\\\
    12 \\\\\\\\\\\\

Leave a reply