Partial Missing Code in Comments of WordPress

10 Mar

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:

It will be modified to:

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:

Finally, active the plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *