Syntax Highlighter plugin for WordPress-MU

For some time now I was looking for a Syntax Highlighter plugin that will work good with WordPress-MU out of the mu-plugins directory. Since I could not find one I decided to modify an existing plugin. I chose the SyntaxHighlighter plugin by Erik Range. This plugin is actually based on code provided by Alex Gorbatchev.

I had to change it quite a bit since I removed the user backend, changed a few hooks and most important made sure that the Java Scripts required by the plugin will be loaded only when there is some code in the post and not for every page loaded. This issue is very important for high volume WPMU sites.

The Syntax Highlighter plugin for WordPress-MU supports the following programming languages:

  • Cpp
  • CSharp
  • Css
  • Delphi
  • Java
  • JScript
  • Php
  • Python
  • Ruby
  • Sql
  • Vb
  • Xml

Known issue: for some reason the plugin works on post view only and not on the home page. will try to fix it soon.

Download: wpmu-syntax-highlighter.zip (beta)

Installation: extract and place in the mu-plugins directory.

Usage: [ source:language] source code comes here [/source]

language is one of the programming languages listed above.

Important: Please note to NOT use a whitespace in the [ source:language] command like shown in Usage and Example below. If you copy & paste it, don’t forget to remove the space.

Example: entering the following code

[ source:php]
// Re-insert source as (formatted) textarea
foreach( $theSources[2] as $sourceID => $sourceBlock ) {
$theBrush =
$theSources[1][$sourceID].$optionString;
$thePost = str_replace(
”{sourceID:{$sourceID}}”,
);
}
[/source]

Will result with this code:

// Re-insert source as (formatted) textarea
foreach( $theSources[2] as $sourceID => $sourceBlock ) {
$theBrush =
$theSources[1][$sourceID].$optionString;
$thePost = str_replace(
”{sourceID:{$sourceID}}”,
);
}




Did you find this post interesting? Please subscribe to my feed.

5 Responses to “Syntax Highlighter plugin for WordPress-MU”

  1. Interesting plugins, good work.

    i have a sugestion for the improvement if you like.
    in the syntaxFooter, you enable all the js from shCore.js to shBrushCpp.js, that the size about 37 kb. but we don’t use it all. maybe just cpp, vb or other but not all in one page.

    When a user ( web surfer ) find your page, he/she download all 37kb but not use all of this.

    the sugestion, maybe you can filter which language use, so just include js that support. for example if the source language just use vb , you just include shCore.js, shBrushVb.js, SyntaxHighlighter.css and clipboard.swf that about 18kb.

    hope help :)

  2. [...] plugins SyntaxHighlighter untuk WPMU ini milik Elad Salomons. Cukup inspiratif. Saya sudah memberi masukan kepada Elad Salomons untuk satu feature pengembangan. Berikut adalah contoh penggunaan untuk SyntaxHighlighter ini. [...]

  3. Agus, thanks, I’m aware of this issue and would like to solve it but still didn’t find the correct way - any suggestions?

  4. In my alternatif, you can change /add some code ini syntaxPost function and syntaxFooter

    in syntaxPost function,
    1. Add global variable just below $IsCodeInPost variable, define as array of codetype, and initilize to zero (0). like below:

    global $CodeType;

    $CodeType = array (”cpp”=> 0, “csharp”=>0, “css” => 0, “delphi” => 0,”java”=>0, “jscript” => 0, “php”=> 0, “python”=>0, “ruby” => 0, “sql” => 0, “vb”=> 0, “xml” => 0);

    2. In loop to search/replace [source] add the following code to set the CodeType match to 1

    foreach( $theSources[2] as $sourceID => $sourceBlock ) {
    $theBrush= ……
    .
    $CodeType[$theBrush] = 1;
    }

    in syntaxFooter: check if $CodeType["vb"] if its value is 1 then include shBrushVb.js and none otherwise.

    ok, hope this help.
    i have practiced it in my pemikiran.com, i will send the modified code if you like.

  5. Yes please, do send over the code to:
    elad dot salomons at gmail dot com

Leave a Reply