在TinyMCE中添加自定义下拉框在WordPress中

时间:2013-04-30 00:05:48

标签: php jquery wordpress tinymce

我一直想把这件事做好几个小时,这让我头疼。

我几乎得到了它,但不幸的是,当没有内容被包装时,它显示了短代码的标题。

例如我的一个标题是短代码1,当它包裹一些文本时,它应该显示[thirdwidth]内容到这里[/ thirdwidth]

但如果我不包装它,它会显示标题。

短代码1 [第三宽度] [/第三宽度]

为什么要这样做?

以下是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);
})();

由于

1 个答案:

答案 0 :(得分:1)

发现问题,我把它留在了:

if(tinyMCE.activeEditor.selection.getContent() == ''){
                            tinyMCE.activeEditor.selection.setContent( v )
                        }