html pre 转义,wordpress pre标签中内容显示转义

想在wordpress 文章中插入代码,小段代码放到code里,大段代码放pre里,但是有时候想插入html代码,发现被浏览器解析了,所以我们要在输出前对代码进行转义,这样pre输出就正常了。

利用WordPress的add_filter钩子实现对文章、评论中的代码经行转义。

食用方法一

将以下code 贴到WordPress主题的funxtions.php中即可。

/**

* wordpress pre html special chars

* @author yefengs

* @link http://os.yefengs.com/wordpress-pre-tag-content-displayed-in-an-escape.html

* @var 1.0.0

* @package WordPress pre

* @subpackage yefengs

* @since 2014-12-22

* @return string */

add_filter('the_content', 'htmlspecialchars_pre', 12);

add_filter('get_comment_text', 'htmlspecialchars_pre');

function htmlspecialchars_pre ($content) {

return preg_replace_callback ("/

(.*?)/si", create_function('$matches','return "" . htmls_pecial_chars($matches[1]) ."";'),$content);

}

function htmls_pecial_chars($content=''){

//htmlspecialchars_decode()

$content = str_replace("

$content = str_replace(">",">",$content);

$content = str_replace("&","&",$content);

$content = str_replace('"',""",$content);

$content = str_replace("'","'",$content);

$content = str_replace(" "," ",$content);

return $content;

}

食用方法二

将以下code 贴到WordPress主题的funxtions.php中即可。

/**

* wordpress pre html special chars

* @author yefengs

* @link http://os.yefengs.com/

* @var 1.0.0

* @package WordPress pre

* @subpackage yefengs

* @since 2014-12-22

* @return string

*/

add_filter('the_content', 'htmlspecialchars_pre', 12);

add_filter('get_comment_text', 'htmlspecialchars_pre');

function htmlspecialchars_pre ($content) {

return preg_replace_callback ("/pre>(.*?)/si", create_function('$matches','return "

" . htmlspecialchars($matches[1]) ."";'),$content);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值