A
A
artem782017-05-07 17:32:48
TinyMCE
artem78, 2017-05-07 17:32:48

Why is the button not being added to the editor in Wordpress?

PHP:

<?php
  class BlockHider {
    public static function init() {
      add_action('admin_head', array(__CLASS__, 'addEditorButton'));
    }

    // Кнопка для добавления шорткода в редакторе
    public static function addEditorButton() {
      if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
        return;
      }

      if( ! in_array( $typenow, array( 'post', 'page' ) ) )
        return;

      if ( 'true' == get_user_option( 'rich_editing' ) ) {
        add_filter('mce_external_plugins', array(__CLASS__, 'addEditorScript'));
        add_filter('mce_buttons', array(__CLASS__, 'registerEditorButton'));
      }
    }

    public static function addEditorScript($plugin_array) {
      $plugin_array['hiddenblockButton'] = plugins_url('/js/editor_button.js', __FILE__);
      return $plugin_array;
    }

    public static function registerEditorButton($buttons) {
      array_push($buttons, 'hiddenblockButton');
      return $buttons;
    }
  }
  
  add_action('plugins_loaded', array('BlockHider', 'init'));

editor_button.js:
(function() {
  tinymce.PluginManager.add('hiddenblockButton', function( editor, url ) {
    editor.addButton('hiddenblockButton', {
      text: 'Скрыть блок',
      title: 'Скрывает выбранный участок от поисковых систем',
      //icon: 'icon dashicons-hidden',
      icon: false,
      onclick: function() {
        selection.setContent('[hiddenblock]' + ed.selection.getContent() + '[/hiddenblock]');
      }
    });
  });
})();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
artem78, 2017-05-08
@artem78

I figured it out myself - I forgot to add to the method addEditorButton:
Therefore, the function exited here:

if( ! in_array( $typenow, array( 'post', 'page' ) ) )
        return;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question