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 … 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| // 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 |
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.
Comments
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.
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!
Rich
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!
hans
[…] update 2: found this article that creates a function to convert backslashes into HTML entities as the posts are saved. https://www.tweaking4all.com/web-development/wordpress/preserve-backslash-in-posts/#comment-268277 […]
[…] Code Credit: Tweaking4All.com […]