4 Jul 2010

Syntax Highlighter for WordPress

Author: Chris | Filed under: Code

I’ve been looking for a good way to highlight code syntax on the blog. I looked at changing blog platforms, writing something myself, or just giving up. Last night I came across something that just might work. It is called SyntaxHighlighter and after doing a little checking around I found it was already packaged up into a plugin for WordPress called SyntaxHighlighter Plus

To get things going you need to upload and activate the plugin (simple instructions on the site) then just surround your code with a sourcecode tag like this:

[sourcecode language=’c#’]
code
[/sourcecode]

You can use it to highlight several different languages including c#, css, javascript, sql, html and others. Here is a sample of the c# highlighting:

public void HelloWorld(string name)
{
      string phrase = string.Format("Hello {0}!", name);
      Console.Write(phrase);
}

And there you have it: Code highlighting on a WordPress blog. The view source button doesn’t work, but that is minor and it’s the only issue I have found thus far with the limited testing I have done. [UPDATE: It turns out the view source button does work, there is just something wrong with my browser. Thanks for letting me know Sven!]

You can download the plugin from http://wordpress.org/extend/plugins/syntaxhighlighter-plus/ or if you are on another platform you can get the source files for the original at http://alexgorbatchev.com/SyntaxHighlighter/.

By the way, Happy 4th of July!

Tags:

2 Responses to “Syntax Highlighter for WordPress”

  1. Sven Aelterman Says:

    Chris,
    View source button works fine for me. Maybe it’s a browser issue (I am using IE 8 on Win7)?
    Sven.

  2. Chris Says:

    Thanks Sven. I am on the same platform so who knows. I did jump over and check it on FireFox and it worked there. Oh well.

    Thanks for letting me know. I will update the post accordingly.

Leave a Reply