Sites that frequently publish code examples, such as tutorial sites, need an easy way to display code on their pages. Fortunately for WordPress users, there are several great ways to display codes in your posts. In this tutorial, you’ll learn about:

  • Manually displaying code
  • The WP Code Shield plugin
  • SyntaxHighligter Plus

If you run a tutorial site, or any other website that may require code examples, read on…

Manually Displaying Code

One of the problems most people face when trying to display code in their posts, is how to get it to not execute on the page. To do this, you’ll need to use character entities to replace the actual brackets.

  • &gt; will become <
  • &lt; will become >

Manually changing each little symbol like this can become a tedious task. Luckily, I’ve found a tool that can convert < and > to &gt; and &lt;.

WP Code Shield

This is a plugin which will take any code inside <code> tags and convert the brackets into character entities automatically. You can get the plugin here.

SyntaxHighighter Plus

If you want something more interactive than boring old code, then SyntaxHighlighter Plus is the plugin for you. You can download the plugin here. This plugin supports syntax highlighting in the following languages:

  • Bash
  • C++
  • C#
  • CSS
  • Delphi
  • Java
  • JavaScript
  • PHP
  • Python
  • Ruby
  • SQL
  • VB
  • XML/HTML

If you wanted to highlight PHP, for example, you’d type out [php]your php code here[/php] and save.

SyntaxHighlighter Code

Saving this would produce a code box similar to this:

SyntaxHighlighter Plus View

As you can see, this plugin produced a feature-filled code box with the ability to copy and print. Just a note, this plugin cannot be used alongside CodeShield.

Conclusion

As I browsed the WordPress plugin directory, it seems these aren’t the only WordPress code plugins available. Feel free to look around and see if another plugin better suits you.

If you don’t write code in your WordPress posts very often, you’d probably want to use one of the first two methods listed here. If you frequently post code examples, something like SyntaxHighlighter Plus will probably be more beneficial to you and your blog.

I hope you enjoyed this guide. Feel free to subscribe to the feed for more Theme Lab updates.

Subscribe to RSS

By: Leland on Jun. 15

Bookmark and Share

10 Comments »

Comment by KimCool
2008-06-15 12:42:28

i use plugin

 
Comment by PJ Brunet
2008-06-15 15:12:20

I think your screen-capture example there for SyntaxHighighter is misleading. Looks like you wrote some HTML in the visual editor and then switched to the HTML editor because I see escaped stuff like “lt”.

The whole point of SyntaxHighighter is it escapes everything for you so you can simply paste working code into your HTML editor as is. There’s also a way to turn off the line numbers if you think they’re ugly.

One problem with SyntaxHighighter, the “html” mode automatically marks up your code with links out to a reference website. To bypass this “feature” use the “xml” mode.

I use it all the time, here’s a really basic example:

http://www.koolwriting.com/todays-color-steelblue/

 
Comment by Leland
2008-06-15 15:18:18

@PJ Brunet: Yes, I took the screenshot of the code editor after I saved the post. After saving it turned the brackets into the character entities.

By the way, would you mind sharing how to turn off the line numbers? I didn’t come across this feature while testing it.

Thanks for the insightful comment.

 
Comment by Kyle Eslick
2008-06-16 08:40:54

If you paste it into the rich text editor, then switch to HTML/Code view it will convert it for you. Just FYI :)

Comment by ThemeLib.com
2008-07-04 12:15:50

Yes, it is so simple :D

 
 
Comment by Leland
2008-06-17 11:02:05

@Kyle: Hmm..I never use the visual editor so wouldn’t know that. Thanks for the tip!

 
Comment by BANAGO Subscribed to comments via email
2008-06-18 02:45:19

I have been looking for such a plugin, but I have found to else. I do not use them, but I am going to use this for sure.

Thanks Leland!

 
Comment by PJ Brunet
2008-06-19 01:50:48

All I can remember is this plugin has some quirks, it’s a hack of “GeSHi” which makes it kludgy, IMO. Maybe you can deactivate the plugin, go into the plugins folder and edit syntax_whatever.php and change these lines:

$igSHOptionsArr = array(
“ENABLED” => true,
“PLAIN_TEXT” => false,
“PLAIN_TEXT_TYPE” => “inbox”,
“SHOW_LANG_NAME” => false,
“PARSE_COMMENTS” => true,
“LINE_NUMBERS” => false,
“FANCY_NUMBERS” => false,
“LINE_COLOUR_1″ => “#3A6A8B”,
“LINE_COLOUR_2″ => “#26536A”
);

I’d dig around in the code more but it’s late and I’m falling asleep.

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post

Recent Comments

  • Pangeran: AH… So far, I never find such theme that using any encrypted code. Beside, most of theme is from...
  • Peter: Well, people don’t just use it for “protecting” links in templates. It’s ethical to...
  • Pangeran: Ah, I see. So using an encrypted code such as VGhlbWUgYnkgPGEgaHJlZj1cIiNcIj 5DZW5lYjwvYT4gYW5kIDxhIGhyZ...
  • Peter: Ah, from what I know, base64 encoding changes normal readable text into a string of characters which are not...
  • ThemeLib.com: This theme is now available for blogger :) http://themelib.com/2008/08/te ch-junkie-blogger-template/

About The Author

Welcome to Theme Lab. My name is Leland, and I run the place. I write all the tutorials and code all the themes you see here on the site.

For more information about me, feel free to read the about page. If you want to drop me a message, feel free to contact me.

Interested in advertising on Theme Lab? We have advertising available at very reasonable rates. Head on over to the advertising page for further details.

Please read our Privacy Policy and Terms of Service in regards to theme usage.

© 2008 - Theme Lab - Powered by WordPress