如果不使用插件的话,在文章中和评论中是无法插入一些PHP、CSS、HTML、JS等代码的,就算使用了code或pre标签也不行…因为wordpress自身有一个强大的HTML标签过滤系统,你插入的html标签会直接消失~~本文将介绍如何在文章及评论中使用pre标签来插入代码。
add_filter('pre_comment_content', 'yyob_encode_code_in_posts_comments');
add_filter('the_content', 'yyob_encode_code_in_posts_comments');
function yyob_encode_code_in_posts_comments($source) {
$encoded = preg_replace_callback('/<pre>(.*?)<\/pre>/ims',
create_function(
'$matches',
'$matches[1] = preg_replace(
array("/^[\r|\n]+/i", "/[\r|\n]+$/i"), "",
$matches[1]);
return "<pre>" . esc_html( $matches[1] ) . "</pre>";'
),
$source);
if ($encoded)
return $encoded;
else
return $source;
}