之前曾写过一篇 WordPress QuickTags编辑器添加自定义按钮 的文章,是基于修改 wordpress 源文件 wp-includes/js/quicktags.js 来实现的,但由于 wordpress 频繁升级需要重新修改此文件,很不方便,因此今天重新修正一下修改方法,基于修改主题目录下的Functions.php 文件来为编辑器添加自定义按钮!
首先来解释一下,在 quicktags.js 文件中有一个edButton()函数,看下面的函数代码及注释:
function edButton(id, display, tagStart, tagEnd, access, open) {
this.id = id; // 用于唯一标识一个按钮
this.display = display; // 按钮的label
this.tagStart = tagStart; // 开始标记
this.tagEnd = tagEnd; // 结束标记
this.access = access; // access key
this.open = open; // 如果按钮没有结束标记,把它置为 -1
}
下面按照上面的代码格式,添加几个haibor比如常用的按钮:
edButtons[edButtons.length] = new edButton('ed_pre','pre',"
\n","\n",'');
edButtons[edButtons.length] = new edButton('ed_pre_smooth','pre.sm',"
\n","\n",'');
edButtons[edButtons.length] = new edButton('ed_
edButtons[edButtons.length] = new edButton('ed_>','>','>','','',-1);
edButtons[edButtons.length] = new edButton('ed_hr','hr',"\n
\n",'','',-1);
edButtons[edButtons.length] = new edButton('ed_h1','h1',"\n
",'
','');edButtons[edButtons.length] = new edButton('ed_h2','h2',"\n
",'
','');edButtons[edButtons.length] = new edButton('ed_h3','h3',"\n
",'
','');最后将以上内容保存为my-quicktags.js文件并保存在主题includes/目录下面,然后在主题functions.php中加入以下代码引入此文件即可:
add_action('admin_print_scripts', 'my_quicktags');
function my_quicktags() {
wp_enqueue_script(
'my_quicktags',
get_stylesheet_directory_uri().'/includes/my-quicktags.js',
array('quicktags')
);
}
这样,再升级wordpress程序时,就省去修改文件的麻烦了!
PS.如果添加的按钮中包含中文,请一定将my-quicktags.js文件保存为utf-8格式,否则会出现乱码!