- Author:Scott Reilly
- First released:2005-04-14
- Last update:2020-01-19
- Compatibility:WP 4.7 – 5.3.2
- Download:[ zip ]
Preserve formatting of code for display by preventing its modification by WordPress and other plugins…
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
codetags. 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
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
- By default this plugin filters ‘the_content’ (post content), ‘the_excerpt’ (post excerpt), and ‘get_comment_text (comment content)’.
A post containing this within
$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') ―);
Find out more at the plugin’s WordPress Plugin Repository page.
Click to see full-size image.
- Whether installing or updating, whether this plugin or any other, it is always advisable to back-up your data before starting
- Install via the built-in WordPress plugin installer. Or download and unzip
preserve-code-formatting.zipinside the plugins directory for your site (typically
- Activate the plugin through the ‘Plugins’ admin menu in WordPress
- Go to the
Code Formattingadmin 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.
- Write a post with code contained within opening and closing
codetags (using the HTML editor, not the Visual editor).
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.
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. Does this plugin include unit tests?
- Fix: Don’t attempt to handle posts containing a code block
- Change: Note compatibility through WP 5.3+
- Change: Update copyright date (2020)
- 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_loadedaction instead of on load
- Change: Update plugin framework to 049
- Correct last arg in call to
add_settings_field()to be an array
- Wrap help text for settings in
- Only use
labelfor help text for checkboxes, otherwise use
- Ensure a
textareadisplays as a block to prevent orphaning of subsequent help text
- Note compatibility through WP 5.1+
- Update copyright date (2019)
- When resetting options, delete the option rather than setting it with default values
- Prevent double “Settings reset” admin notice upon settings reset
- 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 CHANGELOG.md file and move all but most recent changelog entries into it
- New: Add README.md 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 README.md’s “Support” section into two
- This release consists of minor behind-the-scenes changes.
- Change: Update plugin framework to 046
reset_options()to reference instance variable
- Note compatibility through WP 4.7+
- Update copyright date (2017)
reset_options()resets values saved in the database
reset_caches()to clear caches and memoized data. Use it in
verify_options()with logic extracted from
verify_config()for initializing default option attributes
add_option()to add a new option to the plugin’s configuration
- Add filter ‘sanitized_option_names’ to allow modifying the list of whitelisted option names
- Change: Refactor
- Disregard invalid lines supplied as part of hash option value
disable_update_check()to check for HTTP and HTTPS for plugin update check API URL
- Translate “Donate” in footer message
- New: Add README.md
- Change: Store setting name in constant
- Change: Unit tests:
- Add and update unit tests
- Prevent direct invocation
/tmp/wordpress-tests-librather than erroring out if not defined via environment variable
- Enable more error output for unit tests
- Change: Add GitHub link to readme
- Change: Note compatibility through WP 4.9+
- Change: Drop compatibility with versions of WP older than 4.7
- Change: Update copyright date (2018)
Copyright & DisclaimerCopyright © 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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 WordPress.org 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.