How to Display Code Snippets in WordPress

If you have read a few articles on this website, you probably already know what displaying code snippets in WordPress means as mentioned in this title.

However, if you are new to this page, there is no need to go searching for it, as you can see an example in the code block below.

body {  margin-left: auto;  margin-right: auto;  width: 90%;  background-color: #d0f0f6;  text-align: left;}

Or for another example, it can also look like this

body {
  margin-left: auto;
  margin-right: auto;
  width: 90%;
  background-color: #d0f0f6;
  text-align: left;
}

So, do you understand now what is meant by displaying code snippets in WordPress in this article title?

Before we continue to the methods, you have surely noticed that the two code examples above look different even though they are in the same article.

They look different because the first code snippet uses an additional plugin, and the second code was added directly using built-in WordPress features.

How to Display Code Snippets in WordPress

Since there are two methods you can use, we will discuss both here, whether using a plugin or without one. However, here we will use.

Displaying Code Snippets Without a Plugin in the Block Editor

First, we will discuss how to display code snippets without a plugin, using the block editor (the default WordPress editor).

The first thing you need to do is create a new article or page. Alternatively, you can also edit an existing article.

Next, once you are in the block editor page, type /code in your block editor area, and select the Code tab to start displaying the code snippet.

Image

After selecting the Code tab, you can write your code directly there, and then to see the result of the code snippet, you can try previewing the page you have created.

Image

Displaying Code Snippets Without a Plugin in the Classic Editor

If we use the Code tag in the Block Editor, then in the classic editor we use a different tag, which is the preformatted tag.

Just like when using the Block Editor, first you need to create or edit an article, then once you are on the edit article page, you select the paragraph type you want to use as Preformatted.

Cara Menampilkan Baris Kode di WordPress - Classic Editor - 1

Once you are using preformatted, you can immediately add the code snippet you want to display, and that code snippet will be visible to your visitors after the article is published.

Cara Menampilkan Baris Kode di WordPress - Classic Editor - 2

Displaying Code Snippets in WordPress With a Plugin

Displaying code snippets with a plugin is more recommended because the code you publish will feature highlighting appropriate to the programming language used, be easy to copy, and have a more attractive appearance.

To display code snippets with a plugin, first you can install the Code Block Pro plugin, then activate it.

After the Code Block Pro plugin is active, you can then edit or add a new article using the default WordPress block editor.

To start using Code Block Pro, you can type /code in your block editor, then select the Code Pro.

Cara Menampilkan Baris Kode di WordPress - Plugin - 1

option. To change the programming language for Syntax Highlight, you can click on the programming language used, then select a new programming language in the panel on the right.

Cara Menampilkan Baris Kode di WordPress - Plugin - 2

If you have already selected the programming language used, then you just need to add your code snippet in the Code Block Pro column, as shown in the image below.

Cara Menampilkan Baris Kode di WordPress - Plugin - 3

Those are three easy ways to display code snippets in WordPress, whether using a plugin or without using a plugin at all.

However, if you want the code you share to look more attractive, it is recommended to use a plugin.

Written by

Muhammad Khoirun Nasikin

I enjoy tinkering with WordPress, hosting, VPS, web development, and various small experiments related to websites. I have over 3 years of experience in the web hosting industry.

COMMENTS

Loading...