Using WordPress Visual Editor
WordPress has two editors – visual and text editors. The visual editor is what you generally use for creating the content while the text editor is used to view the source code. Let us take an example of inserting the raw HTML image code for inserting an image.
The raw HTML code for inserting image is as below:
Let us create a test post and paste the above code under “Visual” editor. Publish the post and see the raw HTML code will exactly appear as it is.
This is sufficient to showcase simple raw codes of few lines.
Using WordPress Text Editor
Now paste the same code under “Text” editor and update the post. When you switch to visual editor, WordPress will automatically convert the code and show the image instead of the raw code.
We also used <code> or <pre> tags to insert the code under “Text” editor. But the result is same that WordPress auto converts the code into image. So basically, you can’t use “Text” editor to insert raw codes into a post.
The above testing indicates that you can only use “Visual” editor to show raw codes as it is. The content pasted on a “Visual” editor is encoded and hence read in the same structure. For example, less than (<) symbol entered under “Visual” tab will be converted to an escape character “<” which can be seen under “Text” tab.
Drawbacks of Showing Code in Visual Editor
There are multiple issues in showing the raw code under “Visual” editor like text content.
- It looks very simple, especially when you have bulk of code it will look congested.
- No line numbers, coloring and formatting.
- You can’t insert shortcodes as it is with square brackets on visual editor.
- When you paste the image URL, WordPress will paste the actual image. So every time you need to press “Undo” to bring the URL back instead of the image or video.
- Straight double quotes will be converted to slanted double quotes to treat it like a text.
In order to overcome these problems, the recommended option to showcase raw code is to use a plugin. There are many plugins out there are we use Crayon Syntax Highlighter due to its ease of use.
Using Crayon Syntax Highlighter
After installing and activate the plugin, it will create a shortcut menu “Settings > Crayon”.
Here you can set global site wide parameters to be effective for all your raw codes.
- Set your default theme, font, height, width and margins.
- Enable or disable the toolbar on top of the raw code.
- Choose one of the 65 available languages as default language style.
- View all posts with crayon syntax highlighting.
- Enable debugging and view log files.
Highlighting Raw Code Block on Post
The plugin will add a “Code” button on the post editor as show below.
Click on this “Code” button to see the “Add Code” popup.
Paste the raw code and customize the settings as per your need. The settings will appear from the global parameters and you can overwrite for individual code blocks. The raw code block will appear like below and you can use dark theme to align with your site’s theme.
Advantages of Using Syntax Highlighter
The biggest advantage of using syntax highlighter is that all raw codes on your site will have uniform look. It also has the following additional benefits:
- Crayon Syntax Highlighter has built-in theme editor to have global settings.
- Inline editor to modify the individual parameter at each code block level.
- The top toolbar has many useful options like toggle line numbers, toggle plain code, copy mode and open the code in a separate browser window.
- You can fix the height of the code block and enable scrollbar within the frame. This will help to display large block of code within smaller height.
- Mobile responsive.