Preserve Code Formatting
Preserve formatting of code for display by preventing its modification by WordPress and other plugins while also retaining whitespace.
The facts
- Rating
- 4.7★ from 3
- Active installs
- 400+
- Price
- Free
- Last updated
- 15 Aug 2025
- Added
- Apr 2005
- Requires WP
- 5.5
- Tested up to
- WP 6.8.8
- Downloads
- 49,680
Our analysis
AI-assistedPreserve Code Formatting is a WordPress plugin designed to maintain the formatting of code snippets in posts and comments. It prevents WordPress and other plugins from altering the code, ensuring that it displays as intended, including preserving whitespace. The plugin is particularly useful for users who have text-based posts created before the introduction of the blocks-based editor.
This plugin does not support posts that use blocks, and it is recommended to avoid using the visual text editor alongside it, as it may conflict with the plugin's functionality. It filters content such as post content, excerpts, and comment text to ensure that embedded HTML tags and entities are rendered accurately in browsers.
Best for: This plugin suits developers or content creators who frequently share code snippets in their posts.
What it does well
- ✓Preserves code formatting in posts and comments
- ✓Prevents WordPress from modifying code snippets
- ✓Retains whitespace in code
- ✓Compatible with text-based posts from before the blocks editor
Where it falls short
- •Does not handle posts containing blocks
- •Not compatible with the visual text editor
Verdict
Preserve Code Formatting is a practical tool for maintaining code integrity in WordPress posts, though its limitations with block posts and the visual editor may affect some users.
From the developer
Scott Reilly's own description of Preserve Code Formatting, lightly tidied.
This plugin preserves formatting of code for display by preventing its modification by WordPress and other plugins while also retaining whitespace.
NOTE: This plugin does NOT handle posts containing blocks. For such posts, use the built-in code or preformatted blocks to format your code for display. However, this plugin would still be needed if you have text-based posts from before using the blocks-based editor.
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.
Notes:
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
brtags). - 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 tags:
$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(—
INSERT INTO $tablepostmeta
(post_id,meta_key,meta_value)
VALUES ('$post_id','link','$extended')
―);
Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage