Learn how to add code examples with the Most Advanced WYSIWYG HTML Editor, TinyMCE.

Code snippets are small blocks of reusable code, it is common for developers to copy and paste code they've already written, code from their company's code base, open source code, and answers on stack overflow. If you have a blog where you share knowledge with other developers, you may probably tested a lot of WYSIWYG editors that provide an easy way to edit your articles, they accomplish their basic task however they doesn't work pretty well with code snippets. Problem that the people that write with markup doesn't have, however what about for those that use HTML ? Fortunately, someone listened our prayers and TinyMCE decided to provide support for code snippets with the help of Prism.js. Prism is a lightweight, extensible syntax highlighter, built with modern web standards in mind. It’s used in thousands of websites, including some of those you visit daily.

Talk is cheap. Show me the code.

- Linus Torvalds , founder of Linux

How to add a snippet

To add code snippets to your content, you can use the codesample plugin (usually included in the community version). To enable this plugin, you only need to provide the name of the plugin "codesample" in the plugins property of the initialization object and add the action button in the toolbar, identified with the same name "codesample":

    selector: 'textarea',
    plugins: 'codesample',
    toolbar: 'codesample'

Once you add the codesample plugin and the action button in the toolbat, your tinymce should show the new button:

TinyMCE Code sample button


This plugin can be used from the version 4.3 of TinyMCE. Therefore it was in "test stage" and there were several bugs and modifications, however most of them were quickly solved e.g you couldn't load more than 10 languages to highlight.

As you know, Prism.js encourages good author practices. Other highlighters encourage or even force you to use elements that are semantically wrong, like <pre> (on its own) or <script>. Prism forces you to use the correct element for marking up code: <code>. On its own for inline code, or inside a <pre> for blocks of code. In addition, the language is defined through the way recommended in the HTML5 draft: through a language-xxxx class. With TinyMCE, this language can be set through a select element shown in the code input dialog. You can add new languages at the initialization by setting the codesample_languages property:

    selector: 'textarea',
    plugins: 'codesample',
    codesample_languages: [
        {text: 'HTML/XML', value: 'markup'},
        {text: 'JavaScript', value: 'javascript'},
        {text: 'CSS', value: 'css'},
        {text: 'PHP', value: 'php'},
        {text: 'Ruby', value: 'ruby'},
        {text: 'Python', value: 'python'},
        {text: 'Java', value: 'java'},
        {text: 'C', value: 'c'},
        {text: 'C#', value: 'csharp'},
        {text: 'C++', value: 'cpp'}
    toolbar: 'codesample'

Now if you click on the codesample button in TinyMCE, then you should see the code input dialog:

TinyMCE Code Sample Input

All unsafe characters for the generated HTML (like a HTML snippet inside) will be automatically escaped to its html symbol, so the HTML generated by TinyMCE with a html snippet should look like:

<p>Span tag in HTML:</p>
<pre class="language-html"><code>&lt;span&gt;Hello World&lt;/span&gt;</code></pre>
<p>Hello World !</p>

And the user interface:

TinyMCE and Prism.js

Customize Prism style

By default TinyMCE uses the default theme of Prism included by default in the plugin. By customizing the codesample_content_css option at the initialization with the path to the custom CSS file of Prism.css you can use your own theme e.g the Our Code World snippet highlighting (Okaidia):

    selector: 'textarea',
    plugins: 'codesample',
    codesample_languages: [
        {text: 'HTML/XML', value: 'markup'},
        {text: 'JavaScript', value: 'javascript'},
        {text: 'CSS', value: 'css'},
        {text: 'PHP', value: 'php'},
        {text: 'Ruby', value: 'ruby'},
        {text: 'Python', value: 'python'},
        {text: 'Java', value: 'java'},
        {text: 'C', value: 'c'},
        {text: 'C#', value: 'csharp'},
        {text: 'C++', value: 'cpp'}
    toolbar: 'codesample',
    // Custom CSS file
    codesample_content_css: "http://ourcodeworld.com/material/css/prism.css",

And as result:

Custom CSS Prism.js TinyMCE code sample

Change code input dimensions

You can change the dimensions of the dialog in which you enter the code by changing the codesample_dialog_width and codesample_dialog_height:

    selector: 'textarea',
    plugins: 'codesample',
    codesample_languages: [
        {text: 'HTML/XML', value: 'markup'},
        {text: 'JavaScript', value: 'javascript'},
        {text: 'CSS', value: 'css'},
        {text: 'PHP', value: 'php'},
        {text: 'Ruby', value: 'ruby'},
        {text: 'Python', value: 'python'},
        {text: 'Java', value: 'java'},
        {text: 'C', value: 'c'},
        {text: 'C#', value: 'csharp'},
        {text: 'C++', value: 'cpp'}
    toolbar: 'codesample',
    // Custom dimensions
    codesample_dialog_height: 400,
    codesample_dialog_width: 600

Custom languages list

So you do not have to look the list of supported Languages by Prism and then create the object with the language identifier and name by yourself, we provide you the list of all supported languages by Prism.js to use in the codesample_languages property of TinyMCE:

    selector: 'textarea',
    plugins: 'codesample',
    toolbar: 'codesample',
    codesample_languages: [

Happy coding !

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.