Page 1 of 1

WordPress – Preserve Backslash in Posts

WordPress – Preserve Backslash in Posts

Preserve backslashes in WordPress brings us to one of the annoying features of WordPress: it strips backslashes.
It has it’s place an reason of course, but it can be a PITA when you’re trying to post code examples.

In this very short article, I’ll show you how to preserve your backslashes with a very small function which replaces each backslash with an HTML entity, which should make it safe to work with in your database and not loose the backslash. Even my code highlighter does not seem to be bothered by it and displays the code correctly (ie. not showing the HTML entity, but rather show the desired backslash).

Note that manually entering the HTML entity in the HTML code of your article (Text view) works only once! When loaded again in the tinyMCE editor, the HTML entity will be converted to a backslash again and posting an update would remove that backslash again. This function prevents the loss of the backslash by doing it’s work each time you save your work.

Preserve Backslash by using HTML Entity

This function is ridiculously simple, and it works great on my own websites.

This is what it does:

When a post is being submitted (new, updated, etc), this function is being called and replaces ALL occurrences of a backslash (\) with it’s HTML entity counter part, which is “#&92; ” (see also our ASCII HTML Table).

The post will be updated and WordPress will strip all backslashes, but not the HTML entities.

When opening the post again for editing, you’ll see the backslashes are there as regular backslashes.

When posting an update, the cycle repeats.

If anyone feels this is bad, wrong or unsafe: please feel free to comment and help us improve!

My Function to Preserve BackSlash

Add the following code to the functions.php file of your active theme.

Please make sure that it’s before any other add_action('save_post', 'somefunction' ); calls in your functions.php. So any “add_action” call with “save_post” in it. Otherwise the backslashes will still be stripped before WordPress gets to your new function and this trick will not work!

If unsure; paste it way in the beginning of your functions.php (after a possible “<?php” of course) – and keep in mind: your file might not have such a call yet.

Note that this post is already prove that it works, plenty of backslashes in this post that stay … 

// T4A Preserve Backslashes in posts

function t4a_keepbackslash($PostID) {
    $thePost = get_post($PostID);
    $Content = str_replace('\\', '\', $thePost->post_content);
    // unhook this function so it doesn'
t loop infinitely
    remove_action( 'save_post', 't4a_keepbackslash' );

    $UpdatedPost = array (
          'ID'           => $PostID,
          'post_title'   => $thePost->post_title,
          'post_content' => $Content
    wp_update_post( $UpdatedPost );
    // re-hook this function
    add_action( 'save_post', 't4a_keepbackslash' );

add_action('save_post', 't4a_keepbackslash' ); // Update Content when saving content


Donation options

Donations are very much appreciated, but not required. Donations will be used for web-hosting expenses, project hardware or a motivational boost (a drink or snack). Thank you very much for those have donated already! It's truly AwEsOmE to see that folks like our articles and small applications.


There is only one comment, you can read it below.
You can post your own comments by using the form below, or reply to existing comments by using the "Reply" button.

Your Comment …

Friendly request to not post large files here (like source codes, log files or config files). Please use the Forum for that purpose.

Please share:
Notify me about new comments (email).
       You can also use your RSS reader to track comments.

Tweaking4All uses the free Gravatar service for Avatar display.
Tweaking4All will never share your email address with others.