Are you looking for a way to automatically escape entitles within the code tags for better displaying code? While there’s probably a plugin for this, we have created a quick code snippet that you can use to escape entitles in comments in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
function escapeEntities($source) { $encoded = preg_replace_callback('/<code>(.*?)<\/code>/ims', create_function('$matches', '$matches[1] = preg_replace(array(”/^[\r|\n]+/i”, “/[\r|\n]+$/i”), “”, $matches[1]); return “<code>” . htmlentities($matches[1]) . “</”.”code>”;’), $source); if ($encoded) return $encoded; else return $source; } add_filter(’pre_comment_content’, ‘escapeEntities’);
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 7 best download manager plugins and how to set up Shopify with WordPress.
Comments Leave a Reply