I just completed 14 Days of WordPress Plugins + 7 Bonus Plugins!

Preserve Code Formatting

Author:Scott Reilly
Version:2.0
Last update:12 April 2008
Compatibility:WP 2.0+, 2.2+, 2.3+ and 2.5+
Comments:go here
Download:[ zip ]
Description:

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

Extended Description

NOTE: Use of the visual text editor will pose problems as it can mangle your intent in terms of <code> tags. I do not offer any support for those who have the visual editor active.

Basically, you can just paste code into <code>, <pre>, and/or other tags you additionally specify and this plugin will:

  • Prevent WordPress from HTML-encoding text (i.e. single- and double-quotes will not become curly; -- and --- will not become en dash and em dash, respectively; ... will not become a horizontal ellipsis, etc)
  • Prevent most other plugins from modifying preserved code
  • Optionally preserve whitespace (in a variety of methods)
  • Optionally preserve code added in comments

Keep these things in mind:

  • ALL embedded HTML tags and HTML entities will be rendered as text to browsers, appearing exactly as you wrote them (including any <br />).
  • By default this plugin filters ‘the_content’ (post content), ‘the_excerpt’ (post excerpt), and ‘get_comment_text (comment content)’.

Example:

A post containing this within <code>&;t/code>:


`$wpdb->query("
        INSERT INTO $tablepostmeta
        (post_id,meta_key,meta_value)
        VALUES ('$post_id','link','$extended')
");

Would, with this plugin enabled, look in a browser pretty much how it does above, instead of like:

$wpdb->query(&#8212;
INSERT INTO $tablepostmeta
(post_id,meta_key,meta_value)
VALUES ('$post_id','link','$extended')
&#8213;);

Screenshot

A screenshot of the plugin’s admin options page.

screenshot 1


Installation

  1. Download the file preserve-code-formatting.zip and unzip it into your wp-content/plugins/ directory.
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress.
  3. Go to the Options → Code Formatting (or in WP 2.5: Settings → Code Formatting) admin options page.
    Optionally customize the options.

Frequently Asked Questions

  • Q: Why does my code still display all funky? (by the way, I’m using the visual editor)

    A: The visual editor has a tendency to screw up some of your intent, especially when you are attempting to include raw code. This plugin does not make any claims about working when you create posts with the visual editor enabled.


Release Log


Copyright & Disclaimer

Copyright © 2004-2008 by Scott Reilly (aka coffee2code)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Acknowledgements

Thanks to all those who have contributed feedback and support!