
If like me, you often display some code on your blog, there’s a strong chance that you’ll enjoy today’s recipe: This handy piece of code will automatically replace all content within <pre> and </pre> tags by html entities.

If like me, you often display some code on your blog, there’s a strong chance that you’ll enjoy today’s recipe: This handy piece of code will automatically replace all content within <pre> and </pre> tags by html entities.
Just paste the following in your functions.php file. Once saved, all your content with <pre> and </pre> tags will be automatically replaced by html entities.
function pre_entities($matches) {
return str_replace($matches[1],htmlentities($matches[1]),$matches[0]);
}
//to html entities; assume content is in the "content" variable
$content = preg_replace_callback('/<pre.*?>(.*?)<\/pre>/imsu',pre_entities, $content);
Credits goes to David Walsh for this very cool tip!
By the way, I recently reviewed the new "WpSEO" plugin on CatsWhoBlog so you should definitely take a look!
Leave a Comment