arthur-eudeline
11/23/2019 - 4:21 PM

WordPress TinyMCE Editor

<?php
/**
 * Liste des paramètres wp_editor() : https://developer.wordpress.org/reference/classes/_wp_editors/parse_settings/
 */ 
wp_editor(
  $editor_content,
  $editor_id,
  array(
    "media_buttons" => false,
    "drag_drop_upload" => false,
    "tinymce" => array(
    	// Les boutons situés dans la toolbar
			"toolbar1" => "formatselect,bold,italic,bullist,numlist,blockquote,alignleft,aligncenter,alignright,link,wp_more,spellchecker,fullscreen,wp_adv",
			'extended_valid_elements' => "test",
          'custom_elements' => "test",
          // Voir https://stackoverflow.com/a/22210333
        )
      )
    );
<?php

add_action('before_wp_tiny_mce', function($editor_params) {
  echo '<pre>';
  var_dump($editor_params);
  echo '</pre>';
});
<?php
// Add a custom button to tiny mce

$tiny_mce_button_slug = "__MY_BUTTON_SLUG__";


// hooks your functions into the correct filters
add_action( 'admin_head', 'accordeon_scene_add_mce_button' );
function accordeon_scene_add_mce_button () {
  // check user permissions
  if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
    return;
  }
  // check if WYSIWYG is enabled
  if ( 'true' == get_user_option( 'rich_editing' ) ) {
    // Enqueue the button js script
    add_filter( 'mce_external_plugins', function ( $plugin_array )
    {
    	global $tiny_mce_button_slug;
      $plugin_array[$tiny_mce_button_slug] = SOMME_1916_SCENE_MANAGER_PLUGIN_URL . '/assets/accordeon-scene-content-mce-button.js';
      return $plugin_array;
    } );
    // add the button to the editors
    add_filter( 'mce_buttons', function( $buttons ) {
    	global $tiny_mce_button_slug;
      array_push( $buttons, $tiny_mce_button_slug );
      return $buttons;
    } );
  }
}

// SI on veux forcer l'ajout du bouton sur un editeur en particulier, pas besoin du filter 'mce_buttons' :
<?php wp_editor( $editor_content,
      $editor_id,
      array(
        "media_buttons" => false,
        "drag_drop_upload" => false,
        "tinymce" => array(
          "toolbar1" => "bold," . $tiny_mce_button_slug // ne met que le bouton bold et custom dans la toolbar
        )
      )
    ); ?>
(function () {
  var tinymceButtonSlug = '__MY_BUTTON_SLUG__';
  tinymce.PluginManager.add(tinymceButtonSlug, function (editor, url) {
    editor.addButton(tinymceButtonSlug, {
      text: 'TEST',
      icon: false,
      onclick: function () {
        // change the shortcode as per your requirement
        editor.insertContent('[wdm_shortcode]');
      }
    });
  });
})();