How to Easily Display Code on Your WordPress Site

Do you run a development related blog and want to share code snippets on your blog posts? By default, showing codes in your WordPress site is not easy. For security reasons WordPress filters out any raw code added into posts, widgets, or comments. We’re often asked about what syntax highlighter plugin do we use to display code on our website. In this article, we will show you how to easily display code on your WordPress site.

First you need to install and activate the Synatx Highlighter Evolved plugin. Upon activation, you need to visit Settings » Syntax Highlighter to configure the plugin settings.

Syntax Highlighter settings page

The basic plugin settings should work for most websites. However, you should review all settings and make changes if necessary. Each option has detailed description to explain what it does. Once you are done, simply click on the save changes button to store your settings.

Syntax Highlighter evolved uses simple shortcodes to display code. For each language you need wrap your code in the shortcode for that language.

For PHP you would wrap your code like this:

[php]

<?php

echo “Hello World”;

?>

[/php]

It will appear in your post like this:

 <?php echo "Hello World"; ?> 

For CSS you will wrap your code like this:

[css]

.entry-title {

font-family:”Open Sans”, arial, sans-serif;

font-size:16px;

color:#272727;

}

[/css]

It will appear on your site like this:

 .entry-title { font-family:"Open Sans", arial, sans-serif; font-size:16px; color:#272727; } 

Syntax Highlighter will automatically highlight the code for that particular language. It will also add line numbers and handle tab indent properly. Your users will be able to easily copy and paste code snippets from your WordPress site.

How to Display Code in WordPress Without Using Plugin

Many bloggers do not run a development blog, so they don’t need to add sample code snippets in their posts very often. For rare occasions, you can add code by encoding the code into HTML entities. Like this:

&gt;?php echo "Hello World"; ?&lt;

The problem with converting code into HTML entities is that it is difficult to do manually. You can use online tools like this one, to convert code into HTML entities.

By converting PHP, HTML, JavaScript code into HTML entities, you can paste them into your WordPress posts. For additional styling you can wrap code between <code> and </code> tags.

We hope this article helped you find the best syntax highlighter plugin for WordPress. You may also enjoy our article about 13 plugins and tips to improve WordPress admin area.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post How to Easily Display Code on Your WordPress Site appeared first on WPBeginner.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s