Replace wordpress Content in PRE Tags with HTML Entities

May 26, 2010 · Print This Article

If you have a website that often display some code on your wordpress, you know the important of converting PRE tag content to their HTML entities.

Doing so prevents worlds of possible rendering issues.

Just paste the following in your functions.php file.

Once saved, it will automatically replace all content within <pre> and </pre> tags by html entities.

//replaces pre content with 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);

Your choice for site templates and wordpress themes


Got something to say?

You must be logged in to post a comment.