WordPress tutorials: Escape entities in comments

Wpsnipp – Adding this code to the functions.php file of your wordpress theme will automatically escape entities within the <code> tags for better displaying code within your posts.Add this to your theme’s functions.php to allow readers post fragments of code in their comments (wrapped in <code>…</code>) which are automatically encoded (think of < and &lt;)



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;
return $source;
add_filter(’pre_comment_content’, ‘escapeEntities’);

Leave a comment