This plugin hasn’t been tested with the latest 3 major releases of WordPress. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.

Preserve Code Formatting


This plugin preserves formatting of code for display by preventing its modification by WordPress and other plugins while also retaining whitespace.

NOTE: Use of the visual text editor will pose problems as it can mangle your intent in terms of code tags. I strongly suggest you not use the visual editor in conjunction with this plugin as I have taken no effort to make the two compatible.


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
  • Prevent shortcodes from being processed
  • 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 tags).
  • By default this plugin filters ‘the_content’ (post content), ‘the_excerpt’ (post excerpt), and ‘get_comment_text (comment content)’.


A post containing this within code tags:

        INSERT INTO $tablepostmeta
        VALUES ('$post_id','link','$extended')

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

INSERT INTO $tablepostmeta
VALUES ('$post_id','link','$extended')

Links: Plugin Homepage | Plugin Directory Page | Author Homepage


  • A screenshot of the plugin's admin options page.


  1. Whether installing or updating, whether this plugin or any other, it is always advisable to back-up your data before starting
  2. Unzip inside the /wp-content/plugins/ directory (or install via the built-in WordPress plugin installer)
  3. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  4. Go to the Settings -> Code Formatting admin settings page (which you can also get to via the Settings link next to the plugin on the Manage Plugins page) and customize the settings.
  5. Write a post with code contained within opening and closing code tags (using the HTML editor, not the Visual editor).


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

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.

Can I put shortcode examples within code tags and not have them be evaluated by WordPress?

Yes, shortcodes within code tags (or any tag processed by this plugin) will be output as pure text and not be processed as shortcodes by WordPress.

Does this plugin include unit tests?




This plugin help me, works fine, thanks for it. Plase keep updated when need it.


Read all 3 reviews

Contributors & Developers

“Preserve Code Formatting” is open source software. The following people have contributed to this plugin.



3.7 (2016-03-29)

* This release largely consists of minor behind-the-scenes changes.

* Change: Update plugin framework to 041
* Change class name to c2c_PreserveCodeFormatting_Plugin_041 to be plugin-specific
* Set textdomain using a string instead of a variable
* Don’t load textdomain from file
* Change admin page header from ‘h2’ to ‘h1’ tag
* Add c2c_plugin_version()
* Formatting improvements to inline docs
* Change: Add support for language packs:
* Set textdomain using a string instead of a variable
* Don’t load textdomain from file
* Remove .pot file and /lang subdirectory
* Remove ‘Domain Path’ from plugin header.
* New: Add LICENSE file.
* New: Add empty index.php to prevent files from being listed if web server has enabled directory listings.
* Change: Declare class as final.
* Change: Explicitly declare methods in unit tests as public or protected.
* Change: Minor tweak to description.
* Change: Minor code reformatting (spacing).
* Change: Minor improvements to inline docs and test docs.
* Change: Note compatibility through WP 4.5+.
* Change: Remove support for WordPress older than 4.1.
* Change: Update copyright date (2016).

3.6 (2015-02-24)

  • Cast some variable as array to avoid potential PHP warnings
  • Add more unit tests
  • Update plugin framework to 039
  • Explicitly declare activation() and uninstall() static
  • Reformat plugin header
  • Minor code reformatting (spacing, bracing)
  • Change documentation links to to be https
  • Minor documentation spacing changes throughout
  • Note compatibility through WP 4.1+
  • Update copyright date (2015)
  • Add plugin icon
  • Regenerate .pot

3.5 (2014-01-11)

  • Add setting to control if code should be preserved in posts (default is true)
  • Don’t wrap ‘pre’ tags in ‘pre’ despite settings values
  • Update plugin framework to 037
  • Better singleton implementation:
    • Add get_instance() static method for returning/creating singleton instance
    • Make static variable ‘instance’ private
    • Make constructor protected
    • Make class final
    • Additional related changes in plugin framework (protected constructor, erroring __clone() and __wakeup())
  • Add unit tests
  • Add checks to prevent execution of code if file is directly accessed
  • Re-license as GPLv2 or later (from X11)
  • Add ‘License’ and ‘License URI’ header tags to readme.txt and plugin file
  • Use explicit path for require_once()
  • Discontinue use of PHP4-style constructor
  • Discontinue use of explicit pass-by-reference for objects
  • Remove ending PHP close tag
  • Minor documentation improvements
  • Minor code reformatting (spacing, bracing)
  • Note compatibility through WP 3.8+
  • Drop compatibility with version of WP older than 3.6
  • Add comments explaining use of base64_encode and base64_decode
  • Update copyright date (2014)
  • Regenerate .pot
  • Change plugin description (to make it shorter)
  • Change donate link
  • Omit final closing PHP tag
  • Add assets directory to plugin repository checkout
  • Update screenshot
  • Move screenshot into repo’s assets directory
  • Add banner


  • Fix bug with settings form not appearing in MS
  • Update plugin framework to 032
  • Remove support for ‘c2c_preserve_code_formatting’ global
  • Note compatibility through WP 3.3+
  • Drop support for versions of WP older than 3.1
  • Change parent constructor invocation
  • Create ‘lang’ subdirectory and move .pot file into it
  • Regenerate .pot
  • Add ‘Domain Path’ directive to top of main plugin file
  • Add link to plugin directory page to readme.txt
  • Add text and FAQ question regarding how shortcodes are prevented from being evaluated
  • Tweak installation instructions in readme.txt
  • Update screenshot for WP 3.3
  • Update copyright date (2012)


  • Fix to properly register activation and uninstall hooks
  • Update plugin framework to version v023
  • Save a static version of itself in class variable $instance
  • Deprecate use of global variable $c2c_preserve_code_formatting to store instance
  • Add __construct(), activation(), and uninstall()
  • Explicitly declare functions public and variable private
  • Remove declarations of instance variable which have since become part of the plugin framework
  • Note compatibility through WP 3.2+
  • Drop compatibility with version of WP older than 3.0
  • Minor code formatting changes (spacing)
  • Update copyright date (2011)
  • Add plugin homepage and author links in description in readme.txt


  • Re-implementation by extending C2C_Plugin_016, which among other things adds support for:
    • Reset of options to default values
    • Better sanitization of input values
    • Offload of core/basic functionality to generic plugin framework
    • Additional hooks for various stages/places of plugin operation
    • Easier localization support
  • Full localization support
  • Change storing plugin instance in global variable to $c2c_preserve_code_formatting (instead of $preserve_code_formatting), to allow for external manipulation
  • Rename class from ‘PreserveCodeFormatting’ to ‘c2c_PreserveCodeFormatting’
  • Remove docs from top of plugin file (all that and more are in readme.txt)
  • Note compatibility with WP 2.9+, 3.0+
  • Drop compatibility with versions of WP older than 2.8
  • Add PHPDoc documentation
  • Minor tweaks to code formatting (spacing)
  • Add package info to top of plugin file
  • Add Upgrade Notice section to readme.txt
  • Update copyright date
  • Remove trailing whitespace
  • Add .pot file


  • Fixed some borked code preservation by restoring some processing removed in previous release


  • Fixed recently introduced bug affecting occasional code preservation by using a more robust alternative approach
  • Fixed “Settings” link for plugin in plugin listing, which lead to blank settings page
  • Changed help text for preservable tags settings input to be more explicit


  • Fix to retain any attributes defined for tags being preserved
  • Fixed recently introduced bug affecting occasional code preservation


  • Fixed newly introduced bug that added unnecessary slashes to preserved code
  • Fixed long-running bug where intended slashes in code got stripped on display (last remaining known bug)


  • Fixed long-running bug that caused some preserved code to appear garbled
  • Updated a lot of internal plugin management code
  • Added ‘Settings’ link to plugin’s plugin listing entry
  • Used plugins_url() instead of hardcoded path
  • Brought admin markup in line with modern conventions
  • Minor reformatting (spacing)
  • Noted compatibility through WP2.8+
  • Dropped support for pre-WP2.6
  • Updated copyright date
  • Updated screenshot


  • 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
  • Moved into its own subdirectory; added readme.txt and screenshot
  • Tested compatibility with WP 2.3.3 and 2.5


  • Initial release