Sep 23rd

When you are a developer and use WordPress…

2010, 23:59 UTC | By | In wordpress
Leave a comment | Trackback  | 2716 views

Every now and then, you stumble over something you did not realize that it exists even though it's so obvious that something like it _must_ exist :)

As a developer, you sometimes need to post source code in your blog. Reading source code can be exciting and rewarding but it can be a pain when the code is not properly formatted. Syntax highlighting is a nice method to improve source code readability and is practically supported by every single development tool that can be used to read or edit code.

It makes sense, because reading highlighted code is a lot easier (and, as a side effect, it also looks better), so naturally, you want this feature to be available when you are frequently posting source code on the web.

Basically, there are 2 methods of achieving this:

  1. server sided parsing and highlighting. There is existing code that will do this for your PHP application - it will take a piece of source code, reformat it and apply syntax highlighting by using HTML or CSS. Problem with it: It can put a significant additional load on your server when code (e.g. from a blog post) is parsed on every request. Caching might improve this though, but there is still load on your server.
  2. client sided parsing and highlighting. This will be much more server friendly as the actual parsing and formatting will happen on the visitor's browser. Most modern browsers have fast enough JavaScript implementations, so performance issues are unlikely to happen. The only price you pay will be somewhat increased traffic when visitors need to download the JavaScript used to apply code highlighting and -formatting.

Syntax Highlighter Evolved is a plugin for WordPress which uses method 2) and does it with a lot of customizable options. It is based on the general purpose JavaScript Syntax Highlighter Library written by Alex Gorbatchev, which, according to his site, is used by many major sites with a need of posting source code.

The library supports basically all major programming languages and is modular enough to minimize the amount of additional traffic - a browser will only download the highlighting code for the languages that is used on a page. So when you post a piece of PHP code, only the PHP highlighter will be requested by your visitors.

I would say, this plugin is a must-have for every blogger with a background in software or web development (it works equally well for HTML and CSS than it does for PHP or C++) and although I'm not a plugin "nut" (I prefer to keep the number of plugins as low as possible), I will definitely keep this one.

Tags:
1 Like Like
 

Subject

  (this is optional)

Comment text

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>