WordPress GeSHi plugin optimization and modification.

Download link at the bottom.

Yesterdays research has pointed me one important issue – the WP-syntax plugin was taking way too long to process the homepage. And it wasn’t only the WP-Syntax but also other syntax-highlighting plugins including WP-GeSHi-highlight and Better WordPress Syntax Highlighter.
From few tested I found that WP-GeSHi-Highlight has the best performance but still – it wasn’t so great.
I was thinking – is there any way to speed up this plugin (or at least minimize the “damage”)?

The plugin itself is very well commented and is very simple, this is the simplified flow of the script:

  1. Plugin init
  2. Loop through posts to display and parse full text looking for <pre lang=”> tag
  3. If found – mark the place with unique identifier
  4. Replace every unique identifier with the GeSHi output

What if one of the posts doesn’t contain <pre lang=”> tag? The plugin still has to parse whole text to look for it. This doesn’t sound good if you want to highlight code only in a fraction of your posts.

Before I did anything else I wanted to run a couple of tests:

WP-GeSHi-Highlight disabled, didn’t parse anything – only initialization.


WP-GeSHi-Highlight enabled, parsed 3 posts.


WP-GeSHi-Highlight enabled, parsed all 10 posts.

After done with the profiling I decided it is worth to modify the plugin.
The new feature works in a very simple way, go to your dashboard and under Settings->WP-GeSHi-Highlight you can set the tag with which posts will be parsed.

The last thing you need to do is to tag all the posts with the code snippets and the plugin will do all the rest for you.

Keep your load times low and your wordpress will love you forever!

You can download the modified plugin here.

Leave a Reply

Your email address will not be published. Required fields are marked *