Opens in a new tab
Scale your business with our modular plugins.
Home » Blog » How to Display Code in WordPress – 3 Methods (with and without plugins)

How to Display Code in WordPress – 3 Methods (with and without plugins)

How to Display Code in WordPress
May 15, 2021||By Juan Mastromarino

Do you want to display bits of code on your site but aren’t sure of the best way to do it? This post is for you. In this guide, we’ll show you different methods to easily display code in WordPress.

Why is it hard to display code in WordPress?

If you have a site where you post useful guides to your users, you may need to showcase code snippets on your blog posts. The thing is that it’s not as simple as pasting the code. Actually, if you paste an HTML snippet in your blog post, for example, it won’t work. As you know, the WordPress text editor is an HTML-based editor, so it automatically processes the code and prints it.

Similarly, if you copy and paste a shortcode, WordPress will display the function instead of simply showing the written shortcode.

This isn’t ideal if you want to display the code and can lead to poor formatting. For example, if you want to embed some HTML code in your article through the WordPress classic editor, the editor will process the code and display it inside the content. 

The best way to fix this issue is to use dedicated styling for your code snippets. And that’s what we are going to show you in this article.

How to display code in WordPress

There are 3 main methods to display code in WordPress:

  1. Using the WordPress editor
    1. Gutenberg
    2. Classic Editor
  2. Using a dedicated WordPress plugin
  3. Inserting the code manually

Let’s have a look at each method and show you how to display code on your site step-by-step.

1) Using the WordPress Editor

The easiest way to display code in WordPress is to use the WordPress Editor. In this section, we’ll show you how to do it using both Gutenberg and the Classic Editor.

1.1) Gutenberg

First, log in to your WordPress website and open the post where you want to display the code. Search for a block called Code and select it.

gutenberg code block

You can start writing your code there and once you’re done, publish the post. In the front end, you will see the code.

gutenberg code

 

This is how you can use the Gutenberg editor to display code on your site.

1.2) Classic Editor

If you’re still using the Classic Editor, you can also display code snippets by using the preformatted text option. First, open the post where you want to include the code using the classic editor.

sidebar css

Then, select the code snippet and choose the preformatted text option from the dropdown.

preformatted text

You’ll see how the text changes. Then, publish the article and check the page from the front end and you will see the code inside a custom box as shown below.

sample css

This way, you can display any code on your WordPress website. 

As you can see, using the WordPress Editor is simple and straightforward. However, if you want more options to display different types of code, you’ll need something else.

2) Display code with a WordPress plugin

Another way to display code in WordPress is by using a dedicated plugin. There are many options out there, but for this demonstration, we’ll use a free tool called SyntaxHighighter Evolved. 

First, you need to install the plugin, so after logging in to your site, go to Plugins > Add New. Search for the SyntaxHighlighter Evolved plugin, install it, and activate it.

install syntax highlighter evolved

Under the WordPress Settings section, you will see the plugin’s configuration options.

display code in wordpress - syntaxhighlighter plugin settings

On the plugin’s settings page, you will see several customization options:

  • Version control
  • Color theme
  • Brushes loading
  • Miscellaneous
  • Additional CSS classes
  • Starting line number
  • Line number padding
  • Tab size
  • Title

display code in wordpress - syntaxhighlighter plugin settings

If you are a basic user, the default configuration is good enough, whereas if you are an advanced user, you may want to have a look at the settings and adjust them to your needs. Once you have changed the settings, save the changes. Under the live preview section, you will see a sample code with a highlighting effect.

display code in wordpress - code preview

At this point, the process varies depending on the editor you’re using. Let’s have a look at both.

2.1) Gutenberg Editor

After activating the plugin, you will see a new block called SyntaxHighlighter Code in the editor.

display code in wordpress - plugin block

You can use that block to display different types of code in your posts. Simply select the block and in the settings panel, you will see the option to choose the language you want to display. You’ll see that there are dozens of languages you can choose from.

block settings

For this example, we will display a CSS code snippet, so we’ll choose CSS from the dropdown.

css language

You can also change the code language from the block section.

block language

After selecting the code language, paste the script in the block.

paste css code

Additionally, you can adjust the plugin’s default settings from the Advanced section in the right panel and change the styling.

syntaxhighlighter evolved settings

Once you’re done, publish (or update) the post and check the post from the front end to see the results.