Skip to content

Instantly share code, notes, and snippets.

@CrisHazzard
Created August 13, 2015 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrisHazzard/1b02a7a9d25b7c8af97d to your computer and use it in GitHub Desktop.
Save CrisHazzard/1b02a7a9d25b7c8af97d to your computer and use it in GitHub Desktop.
<?
//------------------------------------------------------------------------------
// Escape HTML tags in post content
add_filter('the_content', 'escape_code_fragments');
// Escape HTML tags in comments
add_filter('pre_comment_content', 'escape_code_fragments');
function escape_code_fragments($source) {
$encoded = preg_replace_callback('/<code(.*?)>(.*?)<\/code>/ims',
create_function(
'$matches',
'$matches[2] = preg_replace(
array("/^[\r|\n]+/i", "/[\r|\n]+$/i"), "",
$matches[2]);
return "<code" . $matches[1] . ">" . esc_html( $matches[2] ) . "`";'
),
$source);
if ($encoded)
return $encoded;
else
return $source;
}
?>
@CrisHazzard
Copy link
Author

Used on my hiking blog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment