这几乎是代码高尔夫,但这是我能想到的最小的一段代码,它将在可视化编辑器上创建一个按钮,以在<h2>
块
add_filter( \'tiny_mce_before_init\', \'wpse18719_tiny_mce_before_init\' );
function wpse18719_tiny_mce_before_init( $initArray )
{
$initArray[\'setup\'] = <<<JS
[function(ed) {
ed.addButton(\'h2\', {
title : \'H2\',
image : \'img/example.gif\',
onclick : function() {
ed.formatter.toggle( \'h2\' );
}
});
}][0]
JS;
return $initArray;
}
add_filter( \'mce_buttons\', \'wpse18719_mce_buttons\' );
function wpse18719_mce_buttons( $mce_buttons )
{
$mce_buttons[] = \'h2\';
return $mce_buttons;
}
它基于
a TinyMCE code sample 并使用技巧将函数传递为
setup
变量(
which will not be needed in 3.2 anymore).
要向HTML编辑器添加按钮,可以扩展更简单的"quicktags" code 通过将此额外Javascript文件排队:
jQuery( function( $ ) {
var h2Idx = edButtons.length;
edButtons[h2Idx] = new edButton(
\'ed_h2\' // id
,\'h2\' // display
,\'<h2>\' // tagStart
,\'</h2>\' // tagEnd
,\'2\' // access
);
var h2Button = $( \'<input type="button" id="ed_h2" accesskey="2" class="ed_button" value="h2">\' );
h2Button.click( function() {
edInsertTag( edCanvas, h2Idx );
} );
// Insert it anywhere you want. This is after the <i> button
h2Button.insertAfter( $( \'#ed_em\' ) );
// This is at the end of the toolbar
// h2Button.appendTo( $( \'#ed_toolbar\' ) );
} );