Today, I noticed that it is no longer required to escape the backslash (
\), known as the “escape character” on *nix systems, inside the pre HTML tag in order not to be removed by WordPress’ HTML filters. This bug has lived long enough to be considered as a WordPress feature, but the devs have suddenly decided to address it. So, no more pain when using the escape character inside pre tags. Good!! But, what happens to the code that has already been published with escaped backslashes? WordPress is one of those free software projects which leave some really difficult homework to their users from time to time. So, here follows my solution.
After you have upgraded to the latest WordPress version, 2.3.1 at the time of writing, create a dump of your WordPress database:
mysqldump -u wpuser -p --opt --databases mywpdb > mywpdb.sql
After the database has been dumped, those backslash pairs in your posts’ data would appear as four (4) backslashes in the MySQL database dump. This is because this file contains the raw strings of your data.
To get an idea about what a raw string is, launch the python interpreter and test the following:
>>> "string with escaped backslash \\" 'string with escaped backslash \\' >>> r"raw string with escaped backslash \\" 'raw string with escaped backslash \\\\'
All the above python-thing is not required at all, but it is good to know what really happens. As you can see, the escaped backslash is represented by four backslashes in the raw string and this is how it has been written by mysqldump in the
All you have to do in order to repair all that code, while leaving all non-escaped backslashes in place, is to use a good text-stream editor, like sed in the way presented below.
Assuming that you have kept a backup of the dump, use sed to repair it in-place:
sed -i 's/\\\\/\\/g' mywpdb.sql
You can now import the repaired dump back into the MySQL server and replace your data with the corrected one.
mysql -u wpuser -p mywpdb < mywpdb.sql
Your previously escaped backslashes should now appear as single backslashes in your posts, but displayed correctly by WordPress.
I haven’t noticed any problems after performing the above operation. It might not work for you though. Use at your own risk.
If I miss anything, please let me know.
Backslashes inside pre HTML tags in WordPress by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2007 - Some Rights Reserved