Add Custom Buttons to TinyMCE in WordPress


John Smith

I want to add a new button with a popup to TinyMCE. But I never see the button. I may be doing something wrong with this modification. How can I insert a new button on that TinyMCE code?

I have this TinyMCE code to display in Wordpress frontend:

    $qt = '';
if( $this->options[ 'wpcc_edit_in_html' ] ) $qt = array( 'buttons' => 'strong,em,block,del,ul,ol,li,spell,close' );
else {
    $qt = FALSE;
    add_filter( 'wp_default_editor', create_function( '', 'return "tinymce";' ) ); // force visual editor
}
$editor_settings = array(
    'theme_advanced_blockformats' => array( 'h2','h3','p' ),
    'wpautop' => true,
    'media_buttons' => false,
    'tinymce' => array(
        'theme_advanced_buttons1' => 'bold,italic,blockquote,strikethrough,bullist,numlist,spellchecker,|,undo,redo,|,mygallery_button',
        'theme_advanced_buttons2' => '',
        'theme_advanced_buttons3' => '',
        'theme_advanced_buttons4' => ''
    ),
    'quicktags' => $qt
);

and insert new button here:

function filter_mce_button( $buttons ) {
        // add a separation before our button, here our button's id is "mygallery_button"
        array_push( $buttons, '|', 'mygallery_button' );
        return $buttons;
    }

    function filter_mce_plugin( $plugins ) {
        // this plugin file will work the magic of our button
        $plugins['myplugin'] = plugin_dir_url( __FILE__ ) . 'mygallery_plugin.js';
        return $plugins;
    }

    add_filter( 'mce_buttons', array( $this, 'filter_mce_button' ) );
    add_filter( 'mce_external_plugins', array( $this, 'filter_mce_plugin' ) );
Sam Skiro

Read this tutorial. https://www.gavick.com/magazine/adding-your-own-buttons-in-tinymce-4-editor.html#tc-section-4

Very detailed tutorial. But the most basic requirements are as follows: add the following code in functions.php to register and create the button:

function add_container_button() {
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_container_plugin');
 add_filter('mce_buttons_3', 'register_container_button');
}
}
add_action('init', 'add_container_button');


function register_container_button($buttons) {
array_push($buttons, "|", "skizzar_container");
return $buttons;
}

function add_container_plugin($plugin_array) {
$plugin_array['skizzar_container'] = plugin_dir_url( __FILE__ ) . 'shortcodes-js/container.js';;
return $plugin_array;
}

Then you need to create a js file that handles the behavior of the button in the editor (you can see my code is called container.js in the code above. Here is the code of my js file:

(function() {
tinymce.PluginManager.add('skizzar_container', function( editor, url ) {
    editor.addButton( 'skizzar_container', {
        title: 'Add a Container',
        icon: 'icon dashicons-media-text',
        onclick: function() {
editor.windowManager.open( {
    title: 'Container',
    body: [{
        type: 'listbox',
        name: 'style',
        label: 'Style',
        'values': [
            {text: 'Clear', value: 'clear'},
            {text: 'White', value: 'white'},                
            {text: 'Colour 1', value: 'colour1'},
            {text: 'Colour 2', value: 'colour2'},
            {text: 'Colour 3', value: 'colour3'},
        ]
    }],
    onsubmit: function( e ) {
        editor.insertContent( '[container style="' + e.data.style + '"]<br /><br />[/container]');
    }
});
}

    });
});
})();

This will create a popup with a dropdown menu where the user can select a style. Hope this helps

Related


Add custom icon to tinyMCE button

captis I'm trying to add a Font-Awsome icon to a button I've added to tinyMCE, thus: ed.addButton('youtube', { title: 'Add Video' , icon: 'icon-youtube', onclick: function () { //do stuff here... } Using images like the docs is not accep

tinyMCE custom plugin for WordPress

mark I'm just getting into plugin development for Wordpress. Now I have a function that I pass as a filter to "tiny_mce_before_init" with specific variables to change buttons, add custom styles and other things like that. I'm building an "options page" and I w

Add custom text color WordPress 3.9 TinyMCE 4 visual editor

iSaumya I have a code snippet that helps me add some custom colors to the visual editor text color dropdown as well as the default color. I am going to paste the code snippet below. function change_mce_options( $init ) { $default_colours = '000000,993300,333

Add custom icon to tinyMCE button

captiz I'm trying to add a Font-Awsome icon to a button I've added to tinyMCE: ed.addButton('youtube', { title: 'Add Video' , icon: 'icon-youtube', onclick: function () { //do stuff here... } Using images like the docs is not acceptable,

How to Add Custom Shortcode Buttons in TinyMCE Editor WordPress

Harun R Rayhan Hi, I am using the latest version of WordPress. I want to add my shortcode in TinyMCE editor like this image : http://prntscr.com/72ycrs My shortcode is: [my_tabs] [my_tab title = "Tab One Title"]Tab One Content Goes Here[/my_tab] [my_tab title

How to add custom block in tinyMce

Tariq Hussein I want to add a custom format block for TinyMCE, I was able to do it using the following code. style_formats: [ {title : 'Code', block : 'pre', classes : 'pre-code', exact: true},] https://codepen.io/anon/pen/vWRGEg However, by adding this code,

Is it possible to add classes to custom tinyMCE buttons?

Pym Action I have a web page in which I am using jQuery UI with tinyMCE. I've added a custom button for the purpose of dragging a draggable textfield with this button: Code: editor.addButton('drag', { text: 'Drag', icon: false,

Is it possible to add classes to custom tinyMCE buttons?

Pym Action I have a web page in which I am using jQuery UI with tinyMCE. I've added a custom button for the purpose of dragging a draggable textfield with this button: Code: editor.addButton('drag', { text: 'Drag', icon: false,

How to add custom block in tinyMce

Tariq Hussein I want to add a custom format block for TinyMCE, I was able to do it using the following code. style_formats: [ {title : 'Code', block : 'pre', classes : 'pre-code', exact: true},] https://codepen.io/anon/pen/vWRGEg However, by adding this code,

Add custom icon to tinyMCE button

captis I'm trying to add a Font-Awsome icon to a button I've added to tinyMCE, thus: ed.addButton('youtube', { title: 'Add Video' , icon: 'icon-youtube', onclick: function () { //do stuff here... } Using images like the docs is not accep

tinyMCE custom plugin for WordPress

mark I'm just getting into plugin development for Wordpress. Now I have a function that I pass as a filter to "tiny_mce_before_init" with specific variables to change buttons, add custom styles and other things like that. I'm building an "options page" and I w

tinyMCE custom plugin for WordPress

mark I'm just getting into plugin development for Wordpress. Now I have a function that I pass as a filter to "tiny_mce_before_init" with specific variables to change buttons, add custom styles and other things like that. I'm building an "options page" and I w

tinyMCE custom plugin for WordPress

mark I'm just getting into plugin development for Wordpress. Now I have a function that I pass as a filter to "tiny_mce_before_init" with specific variables to change buttons, add custom styles and other things like that. I'm building an "options page" and I w

tinyMCE custom plugin for WordPress

mark I'm just getting into plugin development for Wordpress. Now I have a function that I pass as a filter to "tiny_mce_before_init" with specific variables to change buttons, add custom styles and other things like that. I'm building an "options page" and I w

How to migrate custom buttons and dialogs to TinyMCE 5

Gypsy I have a 3rd party Bootstrap plugin (probably EOL) that adds buttons to the toolbar which in turn open a dialog where Bootstrap elements can be selected to add to the content. It uses the following code: var insertBtn = tinymce.ui.Factory.create({ ty

Add icon to TinyMCE custom menu

work for life I'm modifying a plugin that adds a custom TinyMCE menu in WP. I need to be able to add icons to dropdown menu items and submenu items. I have icons enabled, which add space to it in the HTML, but can't figure out where JS can put them in. createC

Add icon to TinyMCE custom menu

work for life I'm modifying a plugin that adds a custom TinyMCE menu in WP. I need to be able to add icons to dropdown menu items and submenu items. I have icons enabled, which add space to it in the HTML, but can't figure out where JS can put them in. createC

Add custom text color WordPress 3.9 TinyMCE 4 visual editor

iSaumya I have a code snippet that helps me add some custom colors to the visual editor text color dropdown as well as the default color. I am pasting the code snippet below. function change_mce_options( $init ) { $default_colours = '000000,993300,333300,003

How to Add Custom Shortcode Buttons in TinyMCE Editor WordPress

Harun R Rayhan Hi, I am using the latest version of WordPress. I want to add my shortcode in TinyMCE editor like this image : http://prntscr.com/72ycrs My shortcode is: [my_tabs] [my_tab title = "Tab One Title"]Tab One Content Goes Here[/my_tab] [my_tab title

How to Add Custom Shortcode Buttons in TinyMCE Editor WordPress

Harun R Rayhan Hi, I am using the latest version of WordPress. I want to add my shortcode in TinyMCE editor with an image like this : http://prntscr.com/72ycrs My shortcode is: [my_tabs] [my_tab title = "Tab One Title"]Tab One Content Goes Here[/my_tab] [my_ta

tinyMCE custom plugin for WordPress

mark I'm just getting into plugin development for Wordpress. Now I have a function that I pass as a filter to "tiny_mce_before_init" with specific variables to change buttons, add custom styles and other things like that. I'm building an "options page" and I w

tinyMCE custom plugin for WordPress

mark I'm just getting into plugin development for Wordpress. Now I have a function that I pass as a filter to "tiny_mce_before_init" with specific variables to change buttons, add custom styles and other things like that. I'm building an "options page" and I w

tinyMCE custom plugin for WordPress

mark I'm just getting into plugin development for Wordpress. Now I have a function that I pass as a filter to "tiny_mce_before_init" with specific variables to change buttons, add custom styles and other things like that. I'm building an "options page" and I w

How to migrate custom buttons and dialogs to TinyMCE 5

Gypsy I have a 3rd party Bootstrap plugin (probably EOL) that adds buttons to the toolbar which in turn open a dialog where Bootstrap elements can be selected to add to the content. It uses the following code: var insertBtn = tinymce.ui.Factory.create({ ty

How to Add Custom Shortcode Buttons in TinyMCE Editor WordPress

Harun R Rayhan Hi, I am using the latest version of WordPress. I want to add my shortcode in TinyMCE editor with an image like this : http://prntscr.com/72ycrs My shortcode is: [my_tabs] [my_tab title = "Tab One Title"]Tab One Content Goes Here[/my_tab] [my_ta

Add custom text color WordPress 3.9 TinyMCE 4 visual editor

iSaumya I have a code snippet that helps me add some custom colors to the visual editor text color dropdown as well as the default color. I am pasting the code snippet below. function change_mce_options( $init ) { $default_colours = '000000,993300,333300,003