wordpress博客内容代码/字符自动转义问题

maolai 网站建设评论173阅读模式

wordpress文章内容分享代码时,在前台查看时文章页代码显示不了或变空白。代码本来是正确的,但当网友复制你的代码添加到主题时出现错误。又比如你分享的图片链接地址是

<img alt='wordpress博客' src='http://img.bokequ.com/wp-content/uploads/2018/11/jzcs.jpg'/>

,但在前台显示的不是上面的链接代码,而是直接显示一张图片等等。

这主要是wordpress网站内容已自动字符转义。如代码标点符号本来是英文的,发布后转成中文标点符号(半角引号会变全角,)。当别人复制你分享的代码时就错误或无法添加了。那么如何解决WordPress文章自动转义字符问题呢。

解决WordPress中字符转义问题网上的方法很多,这里介绍一种,在你主题的functions.php文件中添加以下代码,保存即可。

/** 强制阻止WordPress代码转义
*http://www.bokequ.com/62.html
*/
function git_esc_html($content) {
    $regex = '/(<pre\s+[^>]*?class\s*?=\s*?[",\'].*?prettyprint.*?[",\'].*?>)(.*?)(<\/pre>)/sim';
    return preg_replace_callback($regex, 'git_esc_callback', $content);
}
function git_esc_callback($matches) {
    $tag_open = $matches[1];
    $content = $matches[2];
    $tag_close = $matches[3];
    $content = esc_html($content);
    return $tag_open . $content . $tag_close;
}
add_filter('the_content', 'git_esc_html', 2);
add_filter('comment_text', 'git_esc_html', 2);
add_filter('asgarosforum_filter_post_content', 'git_esc_html', 2);

二、也可以在你的wordpress主题里面的functions.php函数文件(如果文章有js代码分享,显示不了代码,推荐上面那种),在文件末尾添加以下代码:

//取消内容转义
remove_filter('the_content', 'wptexturize');
//取消摘要转义
remove_filter('the_excerpt', 'wptexturize');
//取消评论转义
remove_filter('comment_text', 'wptexturize');
//取消标题转换
remove_filter('the_title', 'wptexturize');

 
maolai
  • 本文由 maolai 发表于 2018年6月7日 14:44:59
  • 转载请务必保留本文链接:http://www.bokequ.com/62.html