我已经尝试了好几个小时来完成这项工作,这让我头晕目眩。
我几乎得到了它,但不幸的是,当没有内容被包装时,它会显示短代码的标题。
例如,我的标题之一是短代码 1,当它包裹一些文本时,它应该显示 [thirdwidth] 内容在这里 [/thirdwidth]
但如果我不包装它,它会显示标题。
简码 1[thirdwidth][/thirdwidth]
为什么这样做?
这是php前面的代码:
function register_customcode_dropdown( $buttons ) {
array_push( $buttons, "Shortcodes" );
return $buttons;
}
function add_customcode_dropdown( $plugin_array ) {
$plugin_array['Shortcodes'] = get_template_directory_uri() . '/style/js/TinyMCE_js.js';
return $plugin_array;
}
function customcode_dropdown() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
return;
}
if ( get_user_option('rich_editing') == 'true' ) {
add_filter( 'mce_external_plugins', 'add_customcode_dropdown' );
add_filter( 'mce_buttons', 'register_customcode_dropdown' );
}
}
add_action('init', 'customcode_dropdown');
这是 TinyMCE_js.js 文件
(function() {
tinymce.create('tinymce.plugins.Shortcodes', {
init : function(ed, url) {
},
createControl : function(n, cm) {
if(n=='Shortcodes'){
var mlb = cm.createListBox('Shortcodes', {
title : 'Shortcodes',
onselect : function(v) {
if(tinyMCE.activeEditor.selection.getContent() == ''){
tinyMCE.activeEditor.selection.setContent( v )
}
if(v == 'shortcode 1'){
selected = tinyMCE.activeEditor.selection.getContent();
if( selected ){
//If text is selected when button is clicked
//Wrap shortcode around it.
content = '[thirdwidth]'+selected+'[/thirdwidth]';
}else{
content = '[thirdwidth][/thirdwidth]';
}
tinymce.execCommand('mceInsertContent', false, content);
}
if(v == 'shortcode 2'){
selected = tinyMCE.activeEditor.selection.getContent();
if( selected ){
//If text is selected when button is clicked
//Wrap shortcode around it.
content = '[12]'+selected+'[/12]';
}else{
content = '[12][/12]';
}
tinymce.execCommand('mceInsertContent', false, content);
}
}
});
// Add some menu items
var my_shortcodes = ['shortcode 1','shortcode 2'];
for(var i in my_shortcodes)
mlb.add(my_shortcodes[i],my_shortcodes[i]);
return mlb;
}
return null;
}
});
tinymce.PluginManager.add('Shortcodes', tinymce.plugins.Shortcodes);
})();
谢谢