Amazon.com Widgets

Code Highlighting in WordPress 2.0

Since this is a development blog, one of the things I wanted to be able to do was to post code examples in various languages, and a code highlighting plugin would add a good deal of value to this. Unfortunately, since we’re running a relatively new version of WordPress, several highlighting plug-ins that run under earlier versions are not compatible with WP 2.0. Happily, I found that the GeSHI Syntax Colorer works fine in WP 2.0.

Based on the Nigel McNie’s GeSHI (Generic Syntax Highlighter), the GeSHI Syntax Colore WP plug-in turned out to be a snap to install. The plug-in was also pretty easy to extend. After about 5 minutes, I was able to add line numbering and place the code in a white block. This was done with the following two lines of php:

  1. $geshi->set_header_type(GESHI_HEADER_PRE);
  2. $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);

I also updated my theme CSS with this code:


  1. pre {
  2. clear:both;
  3. overflow:auto;
  4. background-color:#ffffff;
  5. /*width:100% !important;*/
  6. width:450px; /*for IE, which doesn’t obey !important or width:100% on pre */
  7. padding-bottom:0 !important;
  8. padding-bottom:1.5em; /* for IE which doesn’t make room at the bottom of the pre for a horizontal scrollbar */
  9. }
  10.  
  11. code {
  12. color: #003366;
  13. font-family: ‘Courier New’,Courier,monospace;
  14. }

There are apparently many additional options in the GeSHI class, which I’ll explore later. In the meantime, I’m just happy I can post code that looks like, well, source code.


Leave a Comment