Backslashes in WordPress

Feb 25th, 2006   4:55 am

If you’re trying to type backslashes in your posts, and notice that they don’t appear in your post, once saved/published, then here’s the solution.

Last time I’d Googled this, a while ago admittedly, the solution was to type 2 backslashes (so that it’d appear as one in your published post). However when you edited the post (like when you use Save & Continue Editing), then your second backslash would disappear, meaning you’re back to square one.

The proper solution is to use the HTML entity reference instead. LookupTables.com have a handy quick reference.

The HTML reference for a backslash (\) is

\

.. so just type that where you want a backslash, or better still customise your Quicktags in quicktag.js.

I made 3 changes to my quicktags.js:

  1. Add the following code at the beginning of all the existing edButtons[edButtons.length] lines:
    edButtons[edButtons.length] =
    new edButton('ed_backslash'
    ,'\'
    ,''
    ,''
    ,'\\'
    ,-1
    );
  2. Add the following to the existing edShowButon(button, i) function:
    else if (button.id == 'ed_backslash') {
             document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertBackslash(edCanvas);" value="' + button.display + '" />');
    }

    .. which is similar to the first “ed_img” if statement.

  3. Add a new function to the end of the file as follows:
    function edInsertBackslash(myField) {
        edInsertContent( myField, "&#92;" );
    }

That’ll provide a new button (the first of your quicktags) to insert a \, and make life that bit easier! And you can use Access Key \ for it (so in Firefox, you can use ALT+\ to quickly insert a \, so that it’s not too inconvenient!)

References:

css.php