WordPress 的默认编辑器的不好用那是没的说,于是就想到了用 CKEditor 来代替 WordPress 的默认编辑器。
在 WordPress 中安装 CKEditor 很简单,我就不多说了。只要在插件中搜索 "ckeditor",大概第一个结果就是,把它装上,然后 Active 就可以了。
SyntaxHighlighter 是一个在 WordPress 中高亮显示代码的插件。在 WordPress 中安装 SyntaxHighlighter 的方法和安装 CKEditor 一样,只要在插件中心搜索 "SyntaxHighlighter",选择 WP SyntaxHighlighter 安装并 Active 就可以了。
虽然这两个插件都能简单地安装,但是要在 CKEditor 中编辑高亮显示代码却还得通过编写 HTML 来实现,这不得不说是一个悲剧。对于我这种有洁癖的码农来说,这个问题没有一个解决方案就算没完。
好在 3 年前有大虾为 CKEditor 这写了一个叫 Ckeditor-SyntaxHighlight 的插件,帮我解决了这个问题。
安装的方法和官方提供的方法一样,只要将下载来的 ckeditor-syntaxhighlight 插件放到 CKEditor 的 plugins 目录,注意是 $wordpress/wp-content/plugins/ckeditor-for-wordpress/ckeditor/plugins 目录,不是 ckeditor-for-wordpress 目录下的那个 plugins(详见 Quick Installation Guide)
然后辑 CKEditor 的配置文件:$wordpress/wp-content/plugins/ckeditor-for-wordpress/ckeditor.config.js,加入两行代码后改成:
/*
Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
/**
* Documentation:
* http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html
*/
CKEDITOR.editorConfig = function(config) {
// The minimum editor width, in pixels, when resizing it with the resize handle.
config.resize_minWidth = 450;
// Protect PHP code tags (<?...?>) so CKEditor will not break them when
// switching from Source to WYSIWYG.
config.protectedSource.push(/<\?[\s\S]*?\?>/g);
// Define toolbars, you can remove or add buttons.
// List of all buttons is here: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.toolbar_Full
// WordPress basic toolbar
config.toolbar_WordpressBasic = [ [ 'Bold', 'Italic', '-', 'Link', 'Unlink', '-', 'Blockquote' ] ];
// WordPress full toolbar
config.toolbar_WordpressFull = [
['Source'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','SpellChecker', 'Scayt'],
['Undo','Redo','Find','Replace','-','SelectAll','RemoveFormat'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
'/',
['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['BidiLtr','BidiRtl'],
['Link','Unlink','Anchor'],['Code'],
'/',
['Format','Font','FontSize'],
['TextColor','BGColor'],
['Maximize', 'ShowBlocks'],['MediaEmbed'],['Iframe']
];
//IE: remove border of image when is as a link
config.extraCss = "a img { border: 0px\\9; }";
// mediaembed plugin
// config.extraPlugins += (config.extraPlugins ? ',mediaembed' : 'mediaembed' );
// CKEDITOR.plugins.addExternal('mediaembed', ckeditorSettings.pluginPath + 'plugins/mediaembed/');
config.extraPlugins += (config.extraPlugins ? ',syntaxhighlight' : 'syntaxhighlight' );
};
完成以后 CKEditor 的 Toolbar 中就会出现一个漂亮的 “Code” 按钮了。