当然我们有时候很不喜欢用插件,毕竟插件还是影响了wordpress的性能,那我们就用代码来实现:
在我们主题的functions.php文件添加如下代码:
add_filter( 'the_content', 'pre_content_filter', 0 );
/**
* 转换pre标签中的html代码
*
* 使用'the_content'钩子.
*
* @author c.bavota
*/
function pre_content_filter( $content ) {
return preg_replace_callback( '|<pre.*>(.*)</pre|isU' , 'convert_pre_entities', $content );
}
function convert_pre_entities( $matches ) {
return str_replace( $matches[1], htmlentities( $matches[1] ), $matches[0] );
}