Subscribe to the RSS Feed
FreeWordpressThemes.us
Free WordPress Tutorials and Themes.

How to Display Code in WordPress Posts

May 13th, 2014 by admin

One of the most common problem that new WordPress users comes across is displaying code in posts. The problem occurs when a user tries to share a code snippet into their posts. By default WordPress filters out the code and it becomes impossible to read. In this article, I will show how to display code in WordPress posts.

Using HTML Entities to Display Code in WordPress Posts

HTML entities are special code that is parsed by browsers to display the character associated with that particular entity. For example:

  • & becomes &
  • &lt; becomes <
  • &gt; becomes >

Using HTML entities you can display code inside your WordPress posts.

&lt;div class="awesome"&gt; will be displayed in browser like this:

<div class="awesome">

This method is particularly useful if you just want to display a couple of lines of code. However, this method can be difficult to apply manually for multiple lines of code.

To solve this problem you can use an online HTML Entities encoder, like this one. Simply put the code you want to convert and hit the encode button. It will convert your code into HTML entities and you can safely paste that code inside <code></code> tag.

Using Plugin To Display Code in WordPress Posts

If you frequently share code snippets in your posts, then the above mentioned methods are not going to work for you. First thing you need to do is install and activate the SyntaxHighlighter Evolved plugin. It will save you from the hassle of converting code into HTML entities. Not only that it will highlight the code to display it in a much more readable format.

It works out of the box, all you need to do is wrap your content around specific language tags. For example, if you are sharing a code that is mainly in PHP, then you would wrap your code like this:

[php]
<?php
function themelab_demo_code() {
$i = 1;
while ($i <= 10):
echo $i."<br />";
$i++;
endwhile;
}
themelab_demo_code();
?>
[/php]

It will appear on your site, like this:

<?php
function themelab_demo_code() { 
$i = 1;
while ($i <= 10):
    echo $i."<br />";
    $i++;
endwhile;
}
themelab_demo_code();
?>

For different languages there are different aliases that you can use. Some of the most commonly used aliases are:

  • To display HTML code use [html] [/html]
  • For CSS use this: [css][/css]
  • For MySQL use this: [sql][/sql]
  • For Javascript use this: [javascript][/javascript]

I hope this article helped you display code in WordPress posts and pages.

If you liked this article, then join ThemeLab on Twitter and Google+.

The post How to Display Code in WordPress Posts appeared first on ThemeLab.

Written by - Visit Website

Posted in Theme Labs

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.