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


Support Us ...

Your support is very much appreciated, and can be as easy as sharing a link to my website with others, or on social media.

Support can also be done by sponsoring me, and even that can be free (e.g. shop at Amazon).
Any funds received from your support will be used for web-hosting expenses, project hardware and software, coffee, etc.

Thank you very much for those that have shown support already!
It's truly amazing to see that folks like my articles and small applications.

Please note that clicking affiliate links, like the ones from Amazon, may result in a small commission for us - which we highly appreciate as well.


There are 4 comments. You can read them below.
You can post your own comments by using the form below, or reply to existing comments by using the "Reply" button.

  • Aug 25, 2018 - 12:55 AM - Rich - Author: Comment Link

    Thanks for this.  I’m going to test and implement it and eventually roll it into a plug-in for future use… after I go and fix 10 years worth of posts on my tech blog that contained plenty of backslashes at one point.  Argh!



    • Aug 26, 2018 - 4:43 AM - hans - Author: Comment Link

      Hi Rich!

      Glad it’s been of use for you. I know _exactly_ what you mean with cleaning up years of code … it sucks.
      If you ever get to create such a plugin; please feel free to post a link here! 



  • Aug 25, 2018 - 1:00 AM Comment Link

    […] update 2:  found this article that creates a function to convert backslashes into HTML entities as the posts are saved. […]

  • Aug 28, 2018 - 8:40 AM Comment Link

    […] Code Credit: […]

Your Comment …

Do 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.