How To Display Code in WordPress (and Make It Look Pretty)

You can name the custom shortcode whatever you want and create multiple options, like [html] [/html] and [css] [/css], for different coding languages.

CSS shortcode
CSS shortcode

However, we’d like to display the raw code in a blog post, not have it actually render into those blocks.

This brings up a dropdown menu to select a platform and website to publish on. You can also use the Manage Accounts option in that menu to log into a WordPress website before exporting.

Your code now appears inside the box.

This brings up a new window called Enlighter Code Insert.

Have you tried to display code in WordPress in the past? If so, which method worked best for you? Let us know in the comments section below.



The Appearance tab lets you pick a standard theme while also giving you access to adjust elements like:

  • Code indentation
  • Text overflow
  • Linenumbering
  • Line-hover effect
  • RAW-Code on doubleclick
Appearance section of theme customizer
Appearance section of theme customizer

If you are using the Classic WordPress editor, you must go to the Text tab, which is the same as the Code Editor from the Gutenberg Block Editor.

Text tab
Text tab

Step 5: Publish and View the Code

However, you still need to highlight and preserve that code’s formatting. And some code may cause problems with the surrounding content if not preserved properly.

A <br> tag
A break tag

There are several methods to properly display code on WordPress, and it often depends on the WordPress editor being used, the code language you’d like to display, and how you’d like to display and format that code. For example, opting for a plugin will definitely provide a more creative formatting experience than the standard Code block in the WordPress Gutenberg editor.

<code> </code>

Space between code tags
Space between code tags

But by following this tutorial, you’ll learn how to display the raw code itself, not what the code is supposed to show on the frontend.

Step 1: Add a Code Block in WordPress

What’s great about the Code block is that it respects all spaces and tabs you already had within the code snippet. As a result, it shouldn’t look any different from the source you copied it from.

Pasted Snippet in the Code Block to display WordPress code
Pasted Snippet in the Code Block

Step 3: Publish and View Results

<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: beige;}
h1 {color: blue;}
p {color: black;}
</style>
</head>
<body>
<h1>Fun Facts About Otters</h1>
<p>A group of otters in water is called a "raft," since they all link arms to prevent from floating away.</p>
</body>
</html>

Paste the code you’d like to display in the large (unlabeled) field at the bottom.

Paste the code
Paste the code

Click one of the Add Block buttons

You’ll now see the Code Editor rather than the visual Block Editor. Find the area you’d like to display the code and paste your encoded HTML into the editor.

Paste encoded HTML
Paste encoded HTML

Another example is the Classic theme, which is a less minimalistic version of the Enlighter theme with brighter colors and more defined lines.

Classic theme
Classic theme

Step 7: Consider Building a Custom Theme

As a result, site visitors see highlighted lines for all the ones you specified.

Lines highlighted on the frontend
Lines highlighted on the frontend

The <pre> tags are less stable than <code> tags, so you may encounter difficulty depending on the type of code you’re trying to show.

The best markdown editors with both features are:

Switch to the Visual editor to view what it looks like. You’ll notice that the section of text will have a “Preformatted” label, which is precisely what the <pre> tag is meant to do.

Choose preformatted option
Choose preformatted option

Within a few seconds, Ulysses places the entire document into a new WordPress post (you may have to log into your WordPress admin when it pops up).

While drafting a document in Ulysses, type " markdown — that’s two apostrophes — whenever you want to immediately add a code highlighter block inside a document.

You’ll see the code blocks already configured and ready to publish. Click the Publish button to make it live.

Code blocks in WordPress editor
Code blocks in the WordPress editor

You can stylize the Code block however you want, with options to change things like text and background colors.

Edit Color and Background Settings
Edit Color and Background Settings

The Speciallines field highlights any lines you specify. To make this happen, type in line numbers separated by commas.

Special lines
Special lines

Let’s take the <br> tag, for example — without <code> tags, it would simply add a break to your paragraph.

An unwanted break
An unwanted break

You may wonder what would happen if you wrote some code into the WordPress visual editor. After all, you’re not messing with the text or code editors, so shouldn’t your code snippet work just fine?

The Enlighter plugin works slightly different for those still using the Classic WordPress Editor.

This, however, poses a problem for those writing about development and design, since you need a way to properly display the code without it doing its real job, like creating a button or adding styling to a paragraph block.

You can also get an idea of what it looks like before publishing by selecting Preview.

Click Publish to display WordPress code
Click Publish

Install the Enlighter – Customizable Syntax Highlighter plugin on your WordPress site using your preferred plugin installation method.

In addition, some Classic WordPress Editor users may find partering with a plugin easier, since it’s a little trickier to display code in the Classic Editor.

Now it’s time to copy the code you’d like to display and paste it into the box that says, “Write Code…”.

Write or Paste in Code to Display
Write or Paste in Code to Display

To place code snippets within paragraphs, you’d turn to the `` markdown (which looks almost the same, but it’s actually two acute/grave accents instead of apostrophes).

Step 3: Export to WordPress

You should see the original code that was pasted into the encoder, before the tool added HTML encoding elements. As mentioned, there aren’t any styling features with the encoders, so this is a wonderful method for a clean, minimalistic appearance.

Published code
Published code

Method 4: Using a Custom Shortcode

However, you must first set which type of code you’d like to preserve:

  • Pick JavaScript unicode if pasting in code with JavaScript elements.
  • Go with HTML symbols when using HTML.
Pick HTML symbols
Pick HTML symbols

Step 2: Paste and Click to Encode

Go back to WordPress, and open the desired post or page.

<pre> </pre>

Pre tags to display WordPress code
Pre tags

Creating a custom shortcode requires experience editing WordPress theme files, working with PHP code, and potentially creating WordPress plugins. Due to those requirements, making a custom shortcode for WordPress can be difficult for those new to PHP.

To begin, you must choose a “syntax highlighter” plugin, which is a fancy way of saying that the plugin will highlight your source code and keep its formatting intact.

The WordPress Gutenberg editor supports markdown, so you can also find and insert the Code block by typing a forward slash (/) into the editor, then start writing “C” or “Code”. WordPress will then show all relevant blocks, giving you a faster way to insert them.

The Shortcode block from Gutenberg is an option, but during our tests, some code snippets lost key formatting elements or characters.

Text editor
Text editor

This particular code snippet uses HTML styling elements to produce three green content blocks with headers and paragraphs.

Click to Write Code and display WordPress code
Click to Write Code

This reveals the Block sidebar in WordPress. If it doesn’t appear, be sure to click the Settings (gear icon) button in the top-right corner of the WordPress window.

Another way to display code on WordPress is by installing a plugin.

There are also several buttons in the upper-right corner of the code box, including one that presents the code in plain text, without the line numbers.

Plain Text button
Plain Text button

To display your code on your WordPress post or page, finish the process by clicking on the Publish button.

Once you’ve installed the Enlighter plugin, go to any post or page and find the Enlighter Code Insert button in the control panel of the editor.

Enlighter Code Insert
Enlighter Code Insert

The page shows two side-by-side box fields. The one on the left is where to paste your code. The one on the right displays the encoded version to copy and paste into WordPress.

Probably not.

Boring code snippet no more 🪄 Spice it up with these easy steps! 👀Click to Tweet

Switch to the Visual Editor (regardless of whether you’re in Gutenberg or Classic WordPress).

Refrain from trying to use the Custom HTML block for this process, as it doesn’t seem to work.

Go to the Options menu
Go to the Options menu

Click the Copy button.

Copy the code
Copy the code

Step 4: Paste Encoded HTML Into the WordPress Code or Text Editor

You’ll see a small grey space inside the visual content. Click to place your cursor within that grey space. This is where you can paste or type in the code for display.

Grey space to display WordPress code
Grey space to write code

We’ll use the Enlighter plugin in this tutorial since it offers options to tag which types of code you display in your code boxes, along with many other styling features. However, you’re more than welcome to try out the others, as they all mostly do the same thing.

Step 1: Install a Syntax Highlighter Plugin

Content creators who write about coding don’t always want to use large code blocks. Sometimes it makes more sense to include a quick bit of code inside a paragraph.

Go to the W3Docs HTML Encoder website.

Here are some of the unusual results that may occur:

  • The code partially appears, with bits of coding showing on the frontend of the post and others disappearing. This looks unprofessional to visitors; it means your code is not accurate.
  • Nothing at all shows up, with the code completely vanishing from sight on the backend and not showing anything on the frontend of your post.
  • You see odd formatting — often something that’s not user-friendly or publishable for an audience.
  • The code may only partially render

To illustrate a couple of these results, we’re going to use the following HTML code snippet:

To reveal styling options for the block, select the Code block, then choose the Settings (gear icon) button. This opens the Block tab, which only shows the Block settings for your selected block — in this case, the Code block.

Go to Block Settings for Code Formatting and to display WordPress code
Go to Block Settings for Code Formatting

Fill in the post title, schedule, and any other information you’d like, such as categories, tags, and featured images.

The most important part, however, is to set the Status to Draft so as not to publish the post without reviewing it on WordPress first.

The wonderful part about markdown editors that connect with WordPress is that they maintain the code blocks throughout the transfer. So, you’ll see nicely formatted code highlighters ready to publish in WordPress.

The code highlighter remains the same in WordPress
The code highlighter remains the same in WordPress

After activation, the plugin is ready to insert code into any post/page with a Gutenberg block or Classic Editor Insert button.

The Enlighter Plugin to display WordPress code
The Enlighter Plugin

Step 2: Insert the Enlighter Sourcecode Block Into a Post

The process of adding a <pre> tag is just like with the <code> tags, but you have more space to work with when it comes to inserting your code.

You should now see a field with the prompt “Write code…”.

Take a look at the below screenshot. In our example here, nothing has changed from the original code snippet; it has simply been presented on the frontend in a grey box.

Displayed WordPress Code on the Frontend
Displayed Code on the Frontend

Step 4: Consider Formatting the Code Block

To avoid situations like this, we encourage you to instead use one of the methods listed below to display code.

How To Display Code on Your WordPress Site (6 Methods)

Many writers turn to markdown editors due to this rapid content creation process. And luckily, some of those editors offer markdown for code blocks, meaning you can display code within the markdown editor, then send it right off to WordPress for publishing.

You can also:

  • Change the size of the text
  • Add padding and margins to the code box
  • Include a border with a custom width and color
Settings for size, dimensions, and border
Settings for size, dimensions, and border

Browse or type in a keyword for the Enlighten Sourcecode block. Click on that block to insert it into the post.

Enlighter Sourcecode Block to display WordPress code
Enlighter Sourcecode Block

Step 3: Paste Code into the Syntax Highlighter Block

The goal is to build a custom shortcode where you can type or paste code between the opening and closing tags of the shortcode.

Code pasted inside HTML shortcode to display WordPress code
Code pasted inside HTML shortcode

Click Publish or Update, then switch to the frontend to see your code in its original form. Similar to <code> tags, <pre> tags are lousy with formatting, so you’re left with the simplest possible look. However, there are ways to stylize these yourself.

Frontend results to display WordPress code
Frontend results
Tips for Styling <pre> Tags

Once the code highlighter appears, you can type or paste anything you want into it.

Paste to display WordPress code into the Ulysses highlighter
Paste code into the Ulysses highlighter

Creating a custom shortcode does the job of inserting chunks of reusable code without you having to copy and paste. That’s why custom shortcodes offer a solid opportunity for displaying code on WordPress.

HTML encoders preserve the integrity of code formats, particularly those with special characters and tabs. Encoders can handle all types of code, but they then translate the inserted code into HTML-ready code that’s easy to paste into WordPress.

The Enlighter Highlighter block then shows up in the Block Editor, with a title for “Generic Highlighting” and a field to “Insert Sourcecode…

Finally, the third button opens the code in a new window, presenting it in a plain text version of your browser window.

Open Code in New Window
Open Code in New Window

Step 5: Set Language and Line Settings for Code Box

You can, however, opt for a markdown editor with HTML exporting, if you’re not fond of direct WordPress exports.

Here are the results if we paste the code directly into the WordPress Gutenberg Block Editor:

Gutenberg results to display WordPress code
Gutenberg Editor results from HTML code

Perhaps the oldest — yet still incredibly reliable — way to display code on WordPress is by simply adding specific HTML tags around the code snippet. This method is best for when working with the classic WordPress editor, or in any HTML editor.