In case someone doesnโt want to do this in a โplug-in wayโ, here is a guide for TinyMCE 4.x
First of all, you need to define a custom format:
formats: { custom_format: {inline: 'span', styles: {color: "red"}, attributes: {class: 'some_css_class'}} }
Then you need to add a button to the toolbar:
toolbar: "mybutton",
Next, you need to configure the button so that it switches the format:
setup: function(editor) { editor.addButton('mybutton', { text: 'My button', icon: false, onclick: function() { tinymce.activeEditor.formatter.toggle('custom_format') } }); }
In addition, if you want the editor to set the state of the button to indicate the format of the current node, automatically add this to the setup function:
onPostRender: function() { ctrl = this, editor.on('NodeChange', function(e) { ctrl.active(e.element.className == "some_css_class") }); }
Your tinymce.init function should look like this:
tinymce.init({ selector: "textarea", formats: { // Other formats... custom_format: {inline: 'span', styles: {color: "red"}, attributes: {class: 'some_css_class'}} } // Other default toolbars toolbar_n: "mybutton", // Finally, setup your button setup: function(editor) { editor.addButton('mybutton', { text: 'My Button', icon: false, onclick: function() { tinymce.activeEditor.formatter.toggle('custom_format') }, onPostRender: function() { ctrl = this, editor.on('NodeChange', function(e) { ctrl.active(e.element.className == "some_css_class") }); } }); }
Note that I added the class attribute to my custom format. This approach allowed me to define my custom styles in a separate stylesheet file and keep my markup as dry as possible (no inline styles!). Point the content_css parameter to your stylesheet and everything will be fine. However, due to the fact that I use Rails as the back-end and BatmanJS as the front-end (and I'm pretty new to the latter), I could not understand how resource routing works, and in the end I added my own custom styles for the file the default content styles of the tinyMCE skin itself (located in skins/SKIN_NAME/content.min.css ).