WordPress is a powerful content management system that can display many types of media — including many different types of images and videos. Unfortunately, WordPress doesn’t display code snippets very well!
In most cases, you can’t paste code directly into a new post because WordPress can’t tell if you want to process the code or render it. Highlighting code on a page that is surrounded by text can also be inconvenient for visitors to your site.
Fortunately, there are many solutions available for rendering code on a WordPress site. This post will share a few of the best solutions!
Table of Contents
- Inserting code into WordPress posts without a plugin
- Inserting code into WordPress posts with a plugin
- Best WordPress plugins to display code snippets
Although WordPress doesn’t display code snippets very well, it is still possible to add them using the
Of course, there are some issues with using this technique. The main issue is that WordPress will not render certain types of code. For example, if you posted the following snippet into the standard WordPress text editor, it would not render correctly:
WordPress would detect it is PHP code and not display it. You will have to use HTML character entities to display the content as expected. For example, the following is required in text mode to display the snippet correctly:
If you aren’t the best at remembering HTML character entities, you can visit a website that will convert strings for you. You can also switch between visual and text editing modes to add your snippet with
This can be a bit time consuming if you need to add multiple snippets!
Another problem is the fact that the snippet will display using the theme’s styles. This might lead to the inclusion of unusual fonts that aren’t suitable for displaying code. You might not even have styles defined for the code tag, which will result in a very boring looking code that is hard to read.
That last issue with displaying a code snippet using this technique is that it won’t have line numbers. Line numbers are a useful feature if you are discussing a piece of code and want to highlight a particular section.
This is how code and pre tags look when rendering a snippet:
You can display code snippets with more style by using one of the many great plugins out there. Many of these plugins also offer additional functionality that makes it easier to copy the code and share it with other people. Here are five of the best:
SyntaxHighlighter Evolved is one of the most popular WordPress plugins for displaying code snippets. It is simple to use and packed with great features. The main features of this plugin include:
- Advanced syntax highlighting for multiple types of snippets including HTML, CSS and PHP using smart tags.
- Automatically creates HTML character entities
- Line numbers are included on snippets
- Multiple themes are available to change the appearance of your snippets
- You can add your own styles to code snippets
- The code boxes are collapsible
- Double-clicking on a code snippet makes it plain text and highlights the entire snippet is highlighted. This makes copying snippets easier.
Using the plugin is easy. Simply wrap your code snippet in the appropriate smart tag for the language you are using. Here are a few examples:
This resulting code will look something like:
oEmbed Gist is an easy-to-use plugin that lets you embed Gists. A Gist is a short snippet of code created using GitHub’s gist creator. You can create public gists that can be shared with a URL or embedded into your WordPress site using oEmbed Gist. The main features of this plugin include:
- Quickly display Github Gists
- Works by simply pasting a Gist URL
- Can embed multi-file Gists
- Useful view raw feature makes copying code easy
- Line numbers
- You can create revisions of the Gist
This is perhaps one of the easiest plugins for displaying code! Start by creating a Gist. We created a simple PHP Gist at https://gist.github.com/anonymous/7d51286a4cc0d63b4da27282bd2e669e. Copy the URL of the saved Gist and paste it into your post.
The end result looks like this:
Crayon Syntax Highlighter
Crayon Syntax Highlighter is a feature-packed option for displaying code snippets. It allows you to really customise how your code snippets are displayed on your WordPress site.
Its main features include:
- Advanced syntax highlighting including Sublime Text styles (a popular text editor)
- Comes with dozens of pre-made themes
- Multi-language support
- Adds line numbers to your code
- Full customisation of the display box including its size, margins and alignment
- Full customisation of the toolbar, tags, code display options and lines
- Capable of loading local and remote files
- Code marking tools and range limiting of the code included from files
Crayon Syntax Highlighter look for any code that is wrapped using pre tags. It uses the attributes inside the pre tag to determine how to display the snippet. There are many attributes available, as listed on this page. Here are a few examples of how we would render snippets:
This would render the following (using the Sublime theme):
There are dozens of ways to render your code using this plugin. Check out the documentation for more information.
The Pastacode WordPress plugin uses PrismJS, a popular syntax highlighting framework. It’s the one we’re using on this page.
It is a lightweight solution that is packed with features, including:
- Extremely small download size
- Uses semantically correct code tags to display code snippets
- All styling is done via CSS
- Wide browser support
- Highlights specific lines
- Easy to customise or extend styles
- Supports more than a dozen languages
- Can embed code from multiple sources, including Gist, Pastebin, BitBucket and GitHub
Using the plugin is simple. Click on the Pastacode button in the WordPress editor.
You will have the option of importing code from various locations or writing new code. We are going to write a quick PHP script.
Our PHP snippet looks like this:
Pastebin is the most popular website for posting code snippets online. It supports every programming language known to man and has some very useful features like snippet expiration timers. The main features of the Pastebin WordPress plugin include:
- Include Pastebin code snippets with a single line
- Line numbers
- Optional syntax highlighting and support for dozens of languages
- Can also use a shortcode
- Raw code and one-click code download
The Pastebin WordPress plugin functions in a similar way to the oEmbed Gist plugin. You simply paste in the address of your Pastebin code snippet to include it in your post. We created a basic PHP snippet at http://pastebin.com/gb5QtjaY.
Placing this line into a WordPress post results in:
You could also use the shortcode [pastebin id=“gb5QtjaY”]
Thanks for reading How To Show Code Snippets in WordPress. For more WordPress hints and tips, subscribe to our blog or follow us on social media!