wordpress博客內容代碼/字符自動轉義問題
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');
文章來源 http://www.bokequ.com/62.html

浙公網安備 33010602011771號