Preserve Code Formatting

Author: Scott Reilly
Version: 3.9.2
First released: 2005-04-14
Last update: 2020-07-06
Compatibility: WP 4.9 – 5.4.2
Download: [ zip ]

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

Extended Description

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 | GitHub | Author Homepage

Find out more at the plugin’s WordPress Plugin Repository page.


Click to see full-size image.

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

    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. Install via the built-in WordPress plugin installer. Or download and unzip inside the plugins directory for your site (typically wp-content/plugins/)
  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. If you are using the block editor (aka Gutenberg), then this plugin is only useful for maintaining code formatting for posts written before WP 5.0 (or whenever you started creating posts with the block editor). You should be using the built-in code block when including code into the block editor. Otherwise, if you are actively using the classic editor, be sure to use the HTML (aka “Text”) editor and not the “Visual” editor or you’ll encounter formatting issues.

Frequently Asked Questions

Q. Why does my code still display all funky (for instance, I’m seeing `&` in places where I expect to see `&`)?
A. Are you 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.
How to tell if you’re using the visual editor: you’re using what is now referred to as the Classic Editor (the editing experience in WordPress that pre-dates the block editor since WordPress 5.0). Above the post content field and to the right, there is a tab labeled “Visual” and another labeled “Text”. If you’re writing code, you want to use “Text” for such posts and not switch back to “Visual”.

Q. Can I put shortcode examples within code tags and not have them be evaluated by WordPress?
A. 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.

Q. Is this plugin compatible with the code block in the block editor?
A. Yes, in the sense that it doesn’t do anything at all. The code block in the block editor should preserve code formatting without this plugin’s intervention.
(If you have older content that predates the block editor and has not been converted to blocks, you’ll still want to keep this plugin active to preserve code formatting in those older posts. But having this plugin active won’t interfere with the behavior of code blocks.)

Q. Does this plugin include unit tests?
A. Yes.

Release Log

3.9.2 (2020-07-01)

  • This minor release updates its plugin framework, adds a file, updates a few URLs to be HTTPS, expands unit testing, updates compatibility to be WP 4.9 through 5.4+, and minor documentation tweaks.


  • Change: Update plugin framework to 050
    • Allow a hash entry to literally have ‘0’ as a value without being entirely omitted when saved
    • Output donation markup using printf() rather than using string concatenation
    • Update copyright date (2020)
    • Note compatibility through WP 5.4+
    • Drop compatibility with version of WP older than 4.9
  • New: Add and move existing TODO list from top of main plugin file into it (and add more items to the list)
  • Change: Note compatibility through WP 5.4+
  • Change: Drop compatibility for version of WP older than 4.9
  • Change: Tweak FAQ verbiage and add an entry addressing code block compatibility
  • Change: Update installation instruction to clarify its use within the two types of editors and the two classic editor modes
  • Change: Update links to to be HTTPS
  • Unit tests:
    • New: Add test for options_page_description()
    • New: Add tests for default hooks
    • New: Add test for setting name
    • New: Add test to verify shortcodes within preserved tags don’t get replaced
    • Change: Store plugin instance in class variable to simplify referencing it
    • Change: Use HTTPS for link to WP SVN repository in bin script for configuring unit tests (and delete commented-out code)

3.9.1 (2020-01-04)

  • Fix: Don’t attempt to handle posts containing a code block
  • Change: Note compatibility through WP 5.3+
  • Change: Update copyright date (2020)

3.9 (2019-04-26)

  • This release is a minor update that verifies compatibility through WordPress 5.1+ and makes minor behind-the-scenes improvements.


  • Change: Initialize plugin on plugins_loaded action instead of on load
  • Change: Update plugin framework to 049
    • 049:
    • Correct last arg in call to add_settings_field() to be an array
    • Wrap help text for settings in label instead of p
    • Only use label for help text for checkboxes, otherwise use p
    • Ensure a textarea displays as a block to prevent orphaning of subsequent help text
    • Note compatibility through WP 5.1+
    • Update copyright date (2019)
    • 048:
    • When resetting options, delete the option rather than setting it with default values
    • Prevent double “Settings reset” admin notice upon settings reset
    • 047:
    • Don’t save default setting values to database on install
    • Change “Cheatin’, huh?” error messages to “Something went wrong.”, consistent with WP core
    • Note compatibility through WP 4.9+
    • Drop compatibility with version of WP older than 4.7
  • Unit tests:
    • New: Add unit test for settings defaults
    • Change: Update unit test install script and bootstrap to use latest WP unit test repo
    • Change: Use actual setting name in a unit test
  • Change: Cast settings values as either array or bool before use, as/if appropriate
  • New: Add file and move all but most recent changelog entries into it
  • New: Add link to plugin’s page in Plugin Directory
  • Change: Note compatibility through WP 5.1+
  • Change: Wrap function docblocks at roughly 80 characters
  • Change: Update copyright date (2019)
  • Change: Update License URI to be HTTPS
  • Change: Split paragraph in’s “Support” section into two

Copyright & Disclaimer

Copyright © 2005-2020 by Scott Reilly (aka coffee2code)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Discussion / Support

Have any questions, comments, or suggestions? Please provide them via the plugin’s support forum. I’ll do my best to reply in a timely fashion and help as best I can.

Unfortunately, I cannot provide guaranteed support, nor do I provide support via any other means.

Was this plugin useful useful to you? Consider giving it a rating. If you’re inclined to give it a poor rating, please first post to the support forum to give me a chance to address or explain the situation.

17 replies on “Preserve Code Formatting”

[…] Preserve Code Formatting 2.0 Preserve formatting of code for display by preventing its modification by WordPress and other plugins while retaining original whitespace and characters. Often, I want to post code and not content. This plugin stops WordPress’ texturize function (i.e., the way it publishes and stylizes certain characters) to garble up the code formatting. […]