Sometimes, the visitors posted their code in the comment section. That is an awesome way to share and communicate. However, in some cases, partial of the code inside pre/code section is missing. This unexpected feature bothered me for a long time. Today, I fixed it with a modification of a plugin.
Issue: Some code inside pre/code section of comment will be missing, if it is posted by guest. For example, if the guest input the following comment:
1 | <pre> <>& </pre> |
It will be modified to:
1 | <pre> & </pre> |
Cause: WordPress will automatically sterilize the original input. Because < and > are two special html tokens, some of them and probably the content inside the pairs will be removed/modified. As a result, some code is missing.
Solution: escape these three special HTML tokens at the very beginning of the comment process.
Fix: install a WordPress plugin Escape HTML. Then add the filter to the preprocess_comment event at the end as:
1 2 3 | add_filter( 'content_save_pre', 'filterCode', 9 ); add_filter( 'excerpt_save_pre', 'filterCode', 9 ); add_filter( 'preprocess_comment', 'filterCode', 0 ); |
Finally, active the plugin.