Добавить пользовательские кнопки для коротких кодов в редактор WordPress - PullRequest
0 голосов
/ 05 октября 2019

Исходя из этого вопроса Я нашел на другом сайте сети stackoverflow, я хочу добавить несколько кнопок в редакторе WordPress tinyMCE. У меня есть шесть шорткодов, но я не знаю, как поступить. Я изменил код, чтобы он соответствовал моим потребностям, но я не уверен, будет ли он работать и как мне нужно добавить все кнопки в код JavaScript. Может ли кто-нибудь помочь / направить меня, пожалуйста?

Фактическая база кода

class BootstrapTinyMCE{

  public function __construct()
  {
    add_action( 'init', [$this, 'shortcodeButtonInit'] );
  }

  // init process for registering our button

  public function shortcodeButtonInit() {

       //Abort early if the user will never see TinyMCE
       if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true')
            return;

       //Add a callback to regiser our tinymce plugin
       add_filter("mce_external_plugins", "bs_register_tinymce_plugin");

       // Add a callback to add our button to the TinyMCE toolbar
       add_filter('mce_buttons', 'bs_add_tinymce_button');
  }


  //This callback registers our plug-in
  public function bs_register_tinymce_plugin( array $plugin_array ) : array
  {
     $plugin_array['bs_button'] = 'js/shortcodes.js';
     return $plugin_array;
  }

  //This callback adds our button to the toolbar
  public function bs_add_tinymce_button( $buttons )
  {
             //Add the button ID to the $button array
     $buttons[] = "container_button";
     $buttons[] = "col_button";
     $buttons[] = "parallax_button";
     $buttons[] = "swiper_button";
     $buttons[] = "modal_button";

     return $buttons;
  }

}

(function($){
  $(document).ready(function(){
    tinymce.create('tinymce.plugins.bs_shortcodes_plugin', {
    init : function(ed, url) {
            // Register command for when button is clicked
            ed.addCommand('wpbootstrap_insert_shortcode', function() {
                selected = tinyMCE.activeEditor.selection.getContent();

                if( selected ){
                    //If text is selected when button is clicked
                    //Wrap shortcode around it.
                    content =  '[shortcode]'+selected+'[/shortcode]';
                }else{
                    content =  '[shortcode]';
                }

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

        // Register buttons - trigger above command when clicked
        ed.addButton('wpse72394_button', {title : 'Insert shortcode', cmd : 'wpse72394_insert_shortcode', image: url + '/path/to/image.png' });
    },
});

// Register our TinyMCE plugin
// first parameter is the button ID1
// second parameter must match the first parameter of the tinymce.create() function above
tinymce.PluginManager.add('wpse72394_button', tinymce.plugins.wpse72394_plugin);


  });
}(jQuery));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...