Package/plugin/module WordPress

Preserve Code Formatting v2.0

I’d like to announce the official release of the updated Preserve Code Formatting plugin (v2.0) for WordPress.

Preserve formatting of code for display by preventing its modification by WordPress and other plugins while retaining original whitespace and characters.

The plugin’s official homepage is located at :

This release debuts the admin options page for the plugin. The release also brings with it full compatibility with WordPress 2.3+ and 2.5.

This update is part of coffee2code’s 14 Days of Plugins (updated plugin #16).

Comments welcome on this post for this version of the plugin. Comments will be closed once this release has been superseded by another.

Read more for a detailed ChangeLog of the release.

Detailed ChangeLog

These are the detailed changes, which may or may not make sense to you depending on your familiarity with the previous features and internals of the plugin.

  • Completely rewritten
  • Now properly handles code embedded in comments
  • Created its own class to encapsulate plugin functionality
  • Added admin options page under Options → Code Formatting (or in WP 2.5: Settings → Code Formatting). Options are now saved to database, negating need to customize code within the plugin source file.
  • Removed function anti_wptexturize() as the new handling approach negates its need
  • Changed description; updated installation instructions
  • Added compatibility note
  • Updated copyright date and version to 2.0
  • Moved into its own subdirectory; added readme.txt and screenshot
  • Tested compatibility with WP 2.3.3 and 2.5

19 replies on “Preserve Code Formatting v2.0”

I think this plugin works just fine, I like it a lot. WordPress should have its own ‘code preserver’.

But I’m having problems with it. Weird enough, it is converting my code to non-ascii garbage, like this


when it should be

    $devolver = $escribo;

        $devolver = 1;

    return $devolver;

You can see a “live example” here:

Thanks for another great plugin. I am having one small problem though… all my forward slashes seem to stripped out such that a new line becomes ‘n’. ie:
print "\nRunning script...";


print "nRunning script...";

Hi, please edit my first comment and remove the link I put, as I’ve disabled the plugin in my site, waiting for a fix. I’m getting some visitors from here, and I don’t want them to think I’m a spammer 🙂

Anyway, you can see the problem in the code I pasted.


I have been looking for such a plugins for years. It is simple and great.

My issue : ‘\’ disappear in code,

c:\windows in code = c:windows

have a check at

note that using pre+code did have the effect of generatings symbols like above. also with some tags. Out of my hundreds of tags I renounce to us CODE and go to I to avoid the bug of alvin in maybe 2 or 3.

Thanks a lot!

Hey, wonderful plugin, simple are exactly what I want.

Just thought I’d let you know that the automatic upgrade for your code preserve plugin doesn’t work well..instead of installing the newer version of code preserve it installs another plugin..

Just thought I’d let you know.


To preserve the backslashes, you can edit the preserve-code-formatting.php, and replace the lines:

$text = str_replace(array('$', "'"), array('$', '''), stripslashes_deep($text));


$text = str_replace(array('$', "'"), array('$', '''), $text);


$code = "[[{$tag}]]" . base64_encode(stripslashes($match[1])) . "[[/{$tag}]]";


$code = "[[{$tag}]]" . base64_encode($match[1]) . "[[/{$tag}]]";

It works for me.

Hi Scott,
a great plugin indeed. I used it with a recent WordPress MU and it works very well with one small caveat: when combined with WP-Syntax, your plugin eats up the language attribute of <pre lang="ruby">.

I did two small modifications to your plugin which helped. The modified lines are:
254: if (preg_match("/^<{$tag}([^>]*>.*)<\\/{$tag}>/Us", $code, $match)) (moved parenthesis)

279:           $code = "<$tag$data</$tag>";
(removed angle bracket)


Comments are closed.