wordpress代碼高亮插件WP SyntaxHighlighter 優化

WP SyntaxHighlighter 一款很不錯的wordpress 代碼高亮插件。 本站當前使用的代碼高亮插件。 在後臺選擇的是2.1.382版本。能使用代碼複製、打印等小工具。可是他在頁面加載的時候十分的不便。會加載每個你所設定的高亮代碼對應的js。出現
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shCore.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushBash.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushCss.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushJava.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushJScript.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushPerl.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushPhp.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushPowerShell.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushPython.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushRuby.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushSql.js?ver=2.1"></script>
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushXml.js?ver=2.1"></script>
這樣的代碼。每加載一個都會對服務器提交一次請求。不利於網站的加載。 對其優化主要從如下兩個方面: 1) 把所需的 shBrushXxx.js 文件合併到一個 js 文件中,如 shBrushes.js,這樣能夠減小加載那些 js 時 HTTP 鏈接的消耗,若是是 Keep-Alive 還好些。 2) 對合並後的 shBrushes.js 文件進一步壓縮,節約網絡流量,有好的 js 壓縮工具能夠選擇衆多 js 文件壓縮併合並。 當前我就將以上所需的shBrushXxx.js 壓縮到一個文件中。並利用在線的js壓縮工具進行壓縮,獲得一個shBrushMinAll.js。將它放到/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/script/目錄下 而後就是修改咱們的插件文件了。找到wp-syntaxhighlighter.php 這個文件; 1041行註釋代碼:
// echo "<script type=\"text/javascript\" src=\"".$wp_sh_plugin_url.$lib_dir."/scripts/shCore.js?ver=".$wp_sh_lib_ver."\"></script>\n";
1068行註釋代碼:
// foreach ($wp_sh_brush_files as $lang => $val) {
			// 	$brush_file = $val[0];
			// 	$brush_ver = $val[2];
			// 	$brush_enable = $val[3];
			// 	if (($brush_ver == '2.1' || $brush_ver == '1.5') && $brush_enable == 'true') {
			// 		echo "<script type=\"text/javascript\" src=\"".$wp_sh_plugin_url.$lib_dir."/scripts/".$brush_file."?ver=".$wp_sh_lib_ver."\"></script>\n";
			// 	} elseif (($brush_ver == '2.1' || $brush_ver == 'all') && $brush_enable == 'added') {
			// 		echo "<script type=\"text/javascript\" src=\"".$brush_file."?ver=".$wp_sh_lib_ver."\"></script>\n";
			// 	}
			// }
在註釋代碼後面加上:
echo "<script type=\"text/javascript\" src=\"".$wp_sh_plugin_url.$lib_dir."/scripts/shBrushMinAll.js?ver=".$wp_sh_lib_ver."\"></script>\n";
保存完成後。再看看頁面,已經ok了;
<script type="text/javascript" src="http://www.xxku.net/wp-content/plugins/wp-syntaxhighlighter/syntaxhighlighter2/scripts/shBrushMinAll.js"></script>
相關文章
相關標籤/搜索