wordpress下kindeditor代碼高亮bug解決辦法

來源:互聯網
上載者:User

我整理了一下用法,供大家參考。

前台頁面先載入外掛程式指令碼和樣式:

 代碼如下 複製代碼

<link rel="stylesheet" type="text/css" href="[kePath]/plugins/code/prettify.css" />
<script type="text/javascript" src="[kePath]/plugins/code/prettify.js"></script>
<script>
$(function(){ prettyPrint(); });
</script>


後台編輯器聲明的時候加上外掛程式樣式,預設這個參數,編輯器中插入代碼跟普通文字一樣,並且再次編輯的時候被隱藏了,所以這一步很關鍵。

 代碼如下 複製代碼

var editor = KindEditor.create('textarea.editor', {
    cssPath : ['[kePath]/plugins/code/prettify.css']
});

按上面的方法配置代碼高亮是可以的,如果在wordpress後台開啟kindeditor代碼高亮,那麼你發布的代碼,就可以使用google代碼高亮樣式了,但是使用中發現個bug,就是只有在頁面完全載入後才會高亮代碼,在此之前是一片灰,有時個別外鏈的圖片速度很慢,那麼代碼長時間處於灰色的“不可讀”狀態。


原因分析:

由於使用了window.onload,所以只有在頁面完全載入的時候才會執行,包括圖片檔案的下載,而我們的網站使用的大量第三方資源,例如gravatar的頭像,百度分享,多說評論什麼的,說不定什麼時候它們抽個風(gravatar的頭像速度超慢),那麼prettyPrint就一直無法執行,代碼也就一片灰。


修改方法:

開啟/wp-content/plugins/kindeditor-for-wordpress/kindeditor-class.php檔案,找到:

 代碼如下 複製代碼


window.onload = function(){
        prettyPrint();
}

修改為:

$(function(){
        prettyPrint();
})

儲存退出,開啟/wp-content/plugins/kindeditor-for-wordpress/kindeditor.php檔案,找到:

 代碼如下 複製代碼


add_action('wp_enqueue_scripts', array(&$kindeditor, 'add_head_script'));

修改為:


add_action('wp_footer', array(&$kindeditor, 'add_head_script'));

把 Kindeditor 的高亮代碼 js 註冊在底部(注意:不一定要在wp_footer,你可以根據實際情況自己修改,總之在引入 jQuery 之後就好了),儲存。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.