我们在Tinymce中的快捷代码下拉框在WordPress 3.9中不起作用吗?

时间:2014-03-27 作者:Robert

嗨,因为新版本即将发布,我想我会下载它,看看我的主题是否仍然有效。

除了现在显示更长的下拉框外,一切都很好。

这是我们在以前版本中用来显示它的代码。

PHP代码:

function register_ppp_shortcodes( $buttons ) {
   array_unshift( $buttons, "Shortcodes" );
   return $buttons;
}

function add_ppp_shortcodes( $plugin_array ) {
   $plugin_array[\'Shortcodes\'] = get_template_directory_uri() . \'/js/Shortcodes_js.js\';
   return $plugin_array;
}

function ppp_shortcodes() {

   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_ppp_shortcodes\' );
      add_filter( \'mce_buttons\', \'register_ppp_shortcodes\' );
   }

}

add_action(\'init\', \'ppp_shortcodes\');
JS代码:

/*global tinyMCE, tinymce*/
/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:true, undef:true, unused:true, curly:true, browser:true, devel:true, maxerr:50 */
(function() {
"use strict";   

    tinymce.create(\'tinymce.plugins.Shortcodes\', {

        init : function(ed, url) {
          ed = ed;
            url = url;
        },
        createControl : function(n, cm) {

            if(n===\'Shortcodes\'){
                var mtb = cm.createListBox(\'Shortcodes\', {
                     title : \'Shortcodes\',
                     onselect : function(p) {
                        var selected = false;
                        var content = \'\';
                        switch (p){

                        case \'H1 Title\':{

                            var h1titleclass = prompt("Would you like a custom class?", "");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if(h1titleclass != \'\'){
                                h1titleclass = \'class= "\'+h1titleclass+\'"\';
                            }

                            if (selected) {
                                content = \'[h1\'+h1titleclass+\']\' + selected + \'[/h1]\';
                            } else {
                                content = \'[h1\'+h1titleclass+\'][/h1]\';
                            }

                            tinymce.execCommand(\'mceInsertContent\', false, content);

                        } // finished shortcode
                        break;

                        case \'H2 Title\':{

                            var h2titleclass = prompt("Would you like a custom class?", "");

                            selected = tinyMCE.activeEditor.selection.getContent();

                            if(h2titleclass != \'\'){
                                h2titleclass = \'class= "\'+h2titleclass+\'"\';
                            }

                            if (selected) {
                                content = \'[h2\'+h2titleclass+\']\' + selected + \'[/h2]\';
                            } else {
                                content = \'[h2\'+h2titleclass+\'][/h2]\';
                            }

                            tinymce.execCommand(\'mceInsertContent\', false, content);

                        } // finished shortcode
                        break;

                        }   
                     }
                });


                // Add some menu items
                var my_shortcodes = [\'H1 Title\',\'H2 Title\'];

                for(var i in my_shortcodes){
                  if (true) {mtb.add(my_shortcodes[i],my_shortcodes[i]);}
                }

                return mtb;
            }
            return null;
        }


    });
    tinymce.PluginManager.add(\'Shortcodes\', tinymce.plugins.Shortcodes);
})();
谁能告诉我从哪里开始的正确方向吗。

我对tinymce知之甚少,正如你所知:(

谢谢

1 个回复
SO网友:Jörn Lund

我一直在为一个类似的问题纠缠不清。(mce编辑器工具栏中未出现按钮)。这种模式在WP 3.9/tinymce 4.0中对我有效:

tinymce.PluginManager.add( \'thing\' , function( editor ){
    editor.addButton(\'thing\', {
        type: \'listbox\',
        text: \'My listbox\',
        onselect: function(e) {
            // do things...
        },
        values: [
            {text: \'Menu item 1\', value: \'Some text 1\'},
            {text: \'Menu item 2\', value: \'Some text 2\'}
        ]
    });
});
令人遗憾的是,这不是向后兼容的,所以您要么破坏3.8支持,要么需要实现一些版本嗅探。

结束