Disabling WordPress character replacements inside code tags

Update 2009-02-12: This bug appears to have been resolved with WordPress 2.7.1.

I noticed when reviewing a recent post that WordPress was “helpfully” replacing two hyphens with emdash, three dots with ellipsis, apostrophe with curly apostrophe, etc. It does this even between <code> tags – on the second line and beyond. I appreciate that WordPress does this when I’m writing in a journalistic style, but I’m also a programmer and engineer, which means it hinders as often as it helps.

In WordPress 2.6.2, the problem comes from a bug in ./wp-includes/formatting.php. The auto-formatter tries not to do these replacements between code tags, but incorrectly identifies the start of any following tag as the end of the code section. That means a multi-line code section (which has <br> tags breaking each line) will have replacements run on it on all lines after the first.

This bug is found between lines 30 and 43:

One solution is to make code tags behave like pre tags. Change lines 35-39 to the following:

Now code tags will be treated as pre tags – absolutely no replacements except for those necessary for HTML encoding. This does introduce bugs in the case of nested code and pre tags, so don’t put them inside each other.

If you want to disable replacements altogether, just comment out lines 32 and 34 (the str_replace and preg_replace calls).

Aside: writing this article so all the tags would display correctly was an incredible pain.

Tags:

  1. teknologinusantara.com’s avatar

    Hi there, You have done a great job. I will definitely digg it and personally recommend to my friends.
    I’m confident they will be benefited from this website.

    Reply

  2. ICON188’s avatar

    Having read this I believed it was extremely enlightening.
    I appreciate you finding the time and effort to put this information together.
    I once again find myself personally spending a lot of
    time both reading and leaving comments. But so what, it was still worthwhile!

    Reply

  3. https://bandarbolaterpercaya.cabanova.com’s avatar

    It’s enormous that you are getting ideas from this article as well as from our argument made here.

    Reply

  4. Odette’s avatar

    Thanks for finally talking about >tolaris.com . Disabling WordPress character replacements inside code tags <Loved it!

    Reply

Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.