Prismatic

Description

The only 3-in-1 syntax highlighter!

Display beautiful code snippets with Prism.js, Highlight.js, or plain code escaping:

  • Prism.js – Code escape + syntax highlight using Prism.js
  • Highlight.js – Code escape + syntax highlight using Highlight.js
  • Plain Flavor – Code escape without syntax highlight

Check out a demo post using Highlight.js. I also use this plugin at WP-Mix and Plugin Planet for all code snippets 🙂

Prism.js Features

  • Supports 60+ coding languages
  • Choose from all 8 available Prism themes
  • Provides a Gutenberg block for adding code snippets
  • Provides TinyMCE/Visual buttons for adding code snippets
  • Option to enable Prism plugin Line Numbers
  • Option to enable Prism plugin Line Highlight
  • Option to enable Prism plugin Show Language
  • Option to enable Prism plugin Copy Code Button
  • Option to enable Prism plugin Command Line
  • Highlights code in post content, excerpts, and comments
  • Detects language- and lang- class prefixes
  • Limit syntax highlighting to Posts and Pages
  • Highlight single-line and multi-line code
  • Granular control over code escaping
  • Smart loading of CSS & JS assets
  • Support for ACF on single post views

Highlight.js Features

  • Supports 50+ coding languages
  • Choose from all 90+ available Highlight themes
  • Provides a Gutenberg block for adding code snippets
  • Provides TinyMCE/Visual buttons for adding code snippets
  • Customize the Highlight.js init JavaScript
  • Highlights code in post content, excerpts, and comments
  • Limit syntax highlighting to Posts and Pages
  • Highlight multi-line blocks of code
  • Detects language- and lang- class prefixes
  • Enable support for no-prefix class names
  • Granular control over code escaping
  • Smart loading of CSS & JS assets

Plain Flavor Features

  • Enable code escaping for post content, excerpts, and/or comments
  • Enable code escaping on the frontend, Admin Area, or both
  • Provides a Gutenberg block for adding code snippets
  • Provides TinyMCE/Visual buttons for adding code snippets
  • Escapes single-line and multi-line code snippets
  • Escapes <code> tags (based on configuration)

General Features

  • Easy to set up and use
  • Built with WordPress APIs
  • Born of simplicity, no frills
  • Squeaky clean, error-free code
  • Lightweight, fast and flexible
  • Focused on performance and security
  • Loads CSS/JS assets only when required
  • Adheres to HTML coding best practices
  • Works with the Gutenberg Block Editor
  • Regularly updated and “future proof”

Prismatic escapes only the essentials to keep your code clean.

Check out the screenshots for more details!

Privacy

This plugin does not collect or store any user data. It does not set any cookies, and it does not connect to any third-party locations. Thus, this plugin does not affect user privacy in any way.

Prismatic is developed and maintained by Jeff Starr, 15-year WordPress developer and book author.

Support development

I develop and maintain this free plugin with love for the WordPress community. To show support, you can make a donation or purchase one of my books:

And/or purchase one of my premium WordPress plugins:

Links, tweets and likes also appreciated. Thank you! 🙂

Screenshots

  • Prismatic General Settings
  • Prismatic Prism.js Settings
  • Prismatic Highlight.js Settings
  • Prismatic Plain Flavor Settings
  • Prism.js : Twilight theme (choose from 7 Prism.js themes!)
  • Highlight.js : Arduino Light theme (choose from 77 Highlight.js themes!)
  • Highlight.js : Gruvbox Dark theme (choose from 77 Highlight.js themes!)
  • Cleanly escaped code without syntax highlighting (Plain Flavor)
  • Gutenberg Prismatic block (under Formatting menu)
  • Prismatic block showing added code and language select
  • Prismatic TinyMCE/Visual button for adding code snippets
  • Prismatic TinyMCE panel showing added code and selected language

Blocks

This plugin provides 1 block.

  • Prismatic

Installation

Installing Prismatic

  1. Make a backup of your database
  2. Upload the plugin to your blog and activate
  3. Visit the plugin settings to configure options

More info on installing WP plugins

Quick Start Guide

Here is a quick guide to get started with Prismatic:

  1. Activate the plugin and visit the Prismatic settings page
  2. Choose Prism.js or Highlight.js for syntax highlighting
  3. Optionally visit the Prism.js or Highlight.js tab to customize options

You are now ready to go. To add a code snippet to any WP Post or Page:

  • If using Gutenberg Block Editor, click on the Prismatic block
  • If using Classic Editor, click on the Prismatic TinyMCE button

To get a better idea, view the screenshots on the Prismatic homepage.

The Prismatic block or button makes it easy to add your code snippet and choose a language. The plugin automatically will output the correct markup to display your code with syntax highlighting. No code editing required! Note: Advanced usage information provided further down on this page.

Like the plugin?

If you like Prismatic, please take a moment to give a 5-star rating. It helps to keep development and support going strong. Thank you!

Uninstalling

Prismatic cleans up after itself. All plugin settings will be removed from your database when the plugin is uninstalled via the Plugins screen. NOTE that uninstalling the plugin will NOT touch any of your post content. Only the plugin options are removed when the plugin is uninstalled via the Plugins screen.

Restore Default Options

To restore default plugin options, either uninstall/reinstall the plugin or visit the Prismatic General Settings > Restore default plugin options.

Usage: Syntax Highlighting

The Prismatic plugin follows the same conventions used by Prism.js and Highlight.js. Here are the basic steps:

  1. Visit the Prismatic General Settings and choose your library
  2. Visit the settings tab for your chosen library
  3. Choose a theme and configure your options

Once the settings are configured, you can enable syntax highlighting for any code snippet by doing one of the following:

  • Wrap multi-line code with pre & code tags: <pre><code>…</code></pre>
  • Wrap single-line code with code tags: <code>…</code>

The plugin also provides a Prismatic Gutenberg block and TinyMCE buttons. So you can add code snippets with a few clicks easily.

Note: Prism.js highlights both multi-line and single-line code snippets. Highlight.js only supports multi-line code snippets.

With the proper markup in place, you can indicate a specific language by adding a class of language-abc or lang-abc to the <code> tag (where “abc” is the language identifier). For example, to indicate PHP as the language for a single-line code snippet:

<code class="language-php"><?php echo 'Hello world'; ?></code>

Likewise, to indicate HTML as the language for a multi-line code snippet:

<pre><code class="language-html">
    <table>
        <tr>
            <th>Name</th>
            <th>Side</th>
            <th>Role</th>
        <tr>
            <td>Darth</td>
            <td>Dark</td>
            <td>Sith</td>
        </tr>
    </table>
</code></pre>

Alternately, the language class may be placed on the <pre> tag, for example:

<pre class="language-html"><code class="optional">
    <table>
        <tr>
            <th>Name</th>
            <th>Side</th>
            <th>Role</th>
        <tr>
            <td>Darth</td>
            <td>Dark</td>
            <td>Sith</td>
        </tr>
    </table>
</code></pre>

Note: in the previous example, ignore the class="optional" added to the code tag; it is used to prevent markdown from mangling the code example on this web page.

Basically, the prefix of the class names (i.e., “lang-” or “language-“) are the same for Prism.js and Highlight.js. The difference is the language identifier (e.g., “css” or “html”) used to specify each language. Check out the following “About Prism.js” and “About Highlight.js” sections for more information.

Note: In addition to detecting the language- and lang- prefixes, Highlight.js also will try to auto-detect the language without it being specified. Plus as an option, you can enable the Highlight.js setting, “Support no-prefix class names” to enable use of language identifiers without any language- or lang- prefix.

Pro Tip: Language class names work when added to the <pre> tag for both Prism.js and Highlight.js.

Usage: Code escaping

Just like Prism.js and Highlight.js, the Prismatic plugin follows HTML coding standards. To enable code escaping:

  1. Visit the Prismatic General Settings and choose your library
  2. Enable “Code Escaping” via the settings tab of your chosen library
  3. Do one of the following:
    • Wrap multi-line code with pre & code tags: <pre><code>…</code></pre>
    • Wrap single-line code with code tags: <code>…</code>

For example, the code snippets included in either of the following markup examples would be escaped (note that the class attribute is not required for code escaping).

This is a single-line example:

<code class="language-php"><?php echo 'Hello world'; ?></code>

This is a multi-line code example:

<pre><code class="language-html">
    <table>
        <tr>
            <th>Name</th>
            <th>Side</th>
            <th>Role</th>
        <tr>
            <td>Darth</td>
            <td>Dark</td>
            <td>Sith</td>
        </tr>
    </table>
</code></pre>

So what exactly happens when the “Code Escaping” options are enabled? Here is a summary that applies to each section (post content, excerpts, and comments):

  • Frontend only – code snippets are escaped at runtime (no changes made to content in the database)
  • Admin Area only – code snippets are escaped when viewed via the Admin Area (changes will be saved to database if the “Update” or “Publish” button is clicked)
  • Frontend & Admin Area – both of the previous are applied
  • None – all code escaping is disabled

Note that each library — Prism.js, Highlight.js, and Plain Flavor — features its own code-escape settings. So the code-escape settings that are applied depends on the currently active library. Visit the plugin’s General Settings to choose your library. Then visit that library’s tab to configure its code-escape settings.

When code escaping is enabled for either/both the frontend or Admin Area, the plugin makes the following changes to any code contained within <code></code> tags:

&                     replaced with &amp;
<                     replaced with &lt;
>                     replaced with &gt;
"                     replaced with &quot;
'                     replaced with '
\r                    removed
trailing whitespace   removed

These are the only changes made to your code, no other changes are made.

As mentioned, the difference between code escaping on the frontend vs. the Admin Area is that, on the frontend, the above changes are made at runtime and not saved to the database; whereas in the Admin Area, the changes are made when the code is viewed via a content editor, such that any changes made will be saved to the database when the user clicks the “Update” or “Publish” button. Please keep this in mind when choosing your code-escape settings.

Important! As explained, enabling code escaping in the Admin Area may result in the escaped code getting saved in the database. This is fine in most cases, but there may be situations where escaping should only happen at runtime. If that is the case, or if you are unsure, choose the “Frontend only” option for the “Code Escaping” setting. The “Frontend only” option only modifies code when displayed on the frontend and does not save any changes to the database.

Usage: Gutenberg Block Editor

To highlight a code block using Gutenberg:

  1. Select the Prismatic block
  2. Select a code language (via sidebar options)
  3. Add your code and done.

Usage: Classic TinyMCE Editor

To highlight code using the TinyMCE/Visual/Rich-Text Editor:

  1. Click the Prismatic button (looks like <>)
  2. Choose a code language
  3. Add your code and click “OK” button

There also is a Prismatic Quicktag button (“pre”) for those using the Plain-Text editor.

Usage: Prism.js Plugins

Prismatic includes several plugins for Prism.js. You can enable them in the Prism settings. Note that some plugins require action on your part, like adding a class to pre elements. Here is a quick list with links to more information.

Note for adding extra classes: If using the Gutenberg Block Editor, you can add extra classes to your code blocks like so:

  1. On the “Edit Post” screen, add the Prismatic block
  2. Click the 3-dot (options) button for the block
  3. Click “Show more settings”

After these steps, an “Advanced” panel will appear in the sidebar. Toggle it open to find “Additional CSS class(es)”. There you can add any extra classes to your code block (like for Line Numbers or Command Line).

About Prism.js

Prism.js version used in Prismatic plugin: 1.29.0

Prism.js resources

License & Info

/*
    Prism: Lightweight, robust, elegant syntax highlighting
    MIT license https://www.opensource.org/licenses/mit-license.php/
    @author Lea Verou https://lea.verou.me
*/

Supported Languages

Language       Class

Apache         = apacheconf
AppleScript    = applescript
Arduino        = arduino
AVR Assembly   = asmatmel
Bash           = bash
Batch          = batch
C              = c
C#             = csharp
C++            = cpp
C-like         = clike
CoffeeScript   = coffeescript
CSS            = css
D              = d
Dart           = dart
Diff           = diff
Elixir         = elixir
G-code         = gcode
Git            = git
Go             = go
GraphQL        = graphql
Groovy         = groovy
HCL            = hcl
HTML/XML/Etc.  = markup, html, xml, svg, mathml, ssml, atom, rss
HTTP           = http
Ini             &hellip;

FAQ

Can you add another language for Prism.js or Highlight.js?

Yes, feel free to suggest a language

Does this work with Gutenberg Block Editor?

Yes, the plugin provides a “Prismatic” block that makes it easy to add code snippets that will be highlighted on the front-end. Also provides “add code” buttons for the Classic TinyMCE (Visual/Text) Editor. Add code, choose a language, done!

Display syntax-highlighted code inside Block Editor?

If for some reason you want to view syntax-highlighted code inside of the Block Editor, you can do it with the Classic Block:

  1. Select the Classic Block
  2. Click on the Prismatic TinyMCE button
  3. Enter your code and save changes

The code won’t be highlighted initially, but if you refresh the page after making changes, or visit the page again in the future, the code will be displayed with syntax highlighting applied.

How to syntax highlight code inside of ACF field?

As of Prismatic version 2.3, code snippets inside of ACF fields are highlighted automatically. Simply add the required class (e.g., language-php) just like any other code snippet, and the plugin will detect and highlight the code. To also escape the highlighted code, enable escaping for post content via the setting, Prism > Code Escaping > Content. Note: ACF is supported only on single post views.

How to make highlighting work with Autoptimize?

For Prismatic plugin to work with Autoptimize, a script needs to be excluded. For details, check out this post by Nick Coughlin.

How to disable block styles on frontend?

If you are not using Gutenberg Block Editor, you can disable the plugin’s block stylesheet. Simply enable the plugin setting, “Block Styles”. Save changes and done.

FYI: the Prismatic block styles are included via: /prismatic/css/styles-blocks.css

How to escape nested code tags?

Currently the only way to display nested <code> tags is to use the following shortcode:

[prismatic_code][/prismatic_code]

Currently this works only for inline <code> tags. More details here.

Line numbers not working?

Apparently some themes have problems displaying line numbers on highlighted code snippets. A possible solution is to add the following code via theme template or simple plugin:

function prismatic_add_body_class($classes) {

    $classes[] = 'line-numbers';

    return $classes;

}
add_filter('body_class', 'prismatic_add_body_class');

All that’s doing is adding a class named line-numbers to the body tag of your web pages. Should do the trick to get line numbers working on stubborn themes. Original idea posted here.

How to filter the language menu?

Check out Filtered Language Menus with Prismatic WordPress Plugin.

How to disable highlighting code in comments?

Check out Disable Highlighting in Comments with Prismatic WordPress Plugin.

Got a question?

Send any questions or feedback via my contact form

Reviews

Meurzh 1, 2022
Easy to install, easy to configure, works out of the box. Integration of snippets in posts and pages is effortless, the color themes are beautiful. It's fast and light, doesn't add much to page load.
C'hwevrer 7, 2022
I'd tried a bunch of other syntax highlighting plugins before I found Prismatic. All of them worked terribly or didn't work at all. Prismatic is a great choice if you need to add code snippets to your WordPress posts or pages.
Gwengolo 19, 2021 1 reply
I love this plugin, it deserve more download and i hope the devlopment will continue in the future 🙂
Read all 42 reviews

Contributors & Developers

“Prismatic” is open source software. The following people have contributed to this plugin.

Contributors

“Prismatic” has been translated into 2 locales. Thank you to the translators for their contributions.

Translate “Prismatic” into your language.

Interested in development?

Browse the code, check out the SVN repository, or subscribe to the development log by RSS.

Changelog

Thank you to everyone providing feedback! If you like Prismatic, please take a moment to give a 5-star rating. It helps to keep development and support going strong. Thank you!

3.4.2 (2024/03/02)

  • Updates plugin settings page
  • Updates default translation template
  • Improves plugin docs/readme.txt
  • Tests on WordPress 6.5 (beta)

Full changelog @ https://plugin-planet.com/wp/changelog/prismatic.txt