Coolcode这个我想不用介绍了,一个很强大的代码高亮插件。我想很多人都在用或者用过吧。但用起来确那么点麻烦。每次都要手动去添加代码,添加语言、行号是不是关闭这些。一个两个还好办。多了就麻烦了。下面来介绍下怎么样添加一个按钮放到WP的编缉器上。
一:找到:
edButtons[edButtons.length] =
new edButton('ed_code'
,'code'
,'<code>'
,'</code>'
,'c'
);
new edButton('ed_code'
,'code'
,'<code>'
,'</code>'
,'c'
);
在后面添加:
edButtons[edButtons.length] =
new edButton('ed_coolcode'
,'coolcode'
,''
,'
?>
new edButton('ed_coolcode'
,'coolcode'
,''
,'
?>
‘
,’x’
);
二:找到:
else if (button.id == 'ed_link') {
document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertLink(edCanvas, ' + i + ');" value="' + button.display + '" />');
}
document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertLink(edCanvas, ' + i + ');" value="' + button.display + '" />');
}
在后面添加:
else if (button.id == 'ed_coolcode') {
document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertCoolcode(edCanvas, ' + i + ');" value="' + button.display + '" />');
}
document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertCoolcode(edCanvas, ' + i + ');" value="' + button.display + '" />');
}
三:在文件末尾添加:
function edInsertCoolcode(myField, i, defaultLang, defaultLine) {
if (!defaultLang) {
defaultLang = 'php';
}
if (!defaultLine) {
defaultLine = 'off';
}
if (!edCheckOpenTags(i)) {
var codeLang = prompt('输入需要加亮的程序语言', defaultLang);
var codeLine = prompt('是否显示行号(on, off)', defaultLine);
edButtons[i].tagStart = '<' + 'coolcode';
if (codeLang) {
edButtons[i].tagStart = edButtons[i].tagStart + ' lang="'
+ codeLang + '"'
}
if (codeLine) {
edButtons[i].tagStart = edButtons[i].tagStart + ' linenum="'
+ codeLine + '"';
}
edButtons[i].tagStart = edButtons[i].tagStart + '>';
edInsertTag(myField, i);
}
else {
edInsertTag(myField, i);
}
}
if (!defaultLang) {
defaultLang = 'php';
}
if (!defaultLine) {
defaultLine = 'off';
}
if (!edCheckOpenTags(i)) {
var codeLang = prompt('输入需要加亮的程序语言', defaultLang);
var codeLine = prompt('是否显示行号(on, off)', defaultLine);
edButtons[i].tagStart = '<' + 'coolcode';
if (codeLang) {
edButtons[i].tagStart = edButtons[i].tagStart + ' lang="'
+ codeLang + '"'
}
if (codeLine) {
edButtons[i].tagStart = edButtons[i].tagStart + ' linenum="'
+ codeLine + '"';
}
edButtons[i].tagStart = edButtons[i].tagStart + '>';
edInsertTag(myField, i);
}
else {
edInsertTag(myField, i);
}
}
行了。以后要加亮代码的时候点一下按钮,然后填上你要显示的语言、选择是不是显示行号就OK了。非常方便,赶快试试吧!~

我目前升級到 WP2.7 新版本,使用你這個方法修改了 quicktags.js 檔案,可是在加了最後要加在檔案末的那些代碼後,反而無法顯示所有的 quicktags 了,是否此方法不相容 2.7 新版呢?因為這版的 quicktags.js 檔案跟之前 2.6.5 版的有所不同,是否請您查看哪裏有問題,麻煩了,多謝!
另,我以先將此檔案恢復至原檔,暫候您的回覆~
我的wp也是2.7的,用博主的方案出现了跟七楼一样的问题。
望博主能帮忙及时解决此问题,谢谢。
wp2.65按照你的方法还是没有看见按钮啊。
好方法:)已经把你该文的介绍添加到我那文章后面,方便更多人使用,不介意吧。呵呵!
可否把下载文件项也给加上。。
正在找这个
貌似没说修改什么文件
顺路来看看!