Add SyntaxHighlighter Evolved Plugin Third Party Brushes To Your WordPress Installation

wordpress-4SyntaxHighlighter Evolved allows you to easily post syntax-highlighted code to your site without losing it’s formatting or making any manual changes. It uses the SyntaxHighlighter JavaScript package by Alex Gorbatchev. It is prepackaged with a select handful of brushes, which includes several third-party brushes. There are many more third-party brushes that are available and not part of the plug-in package. There are two approaches to adding these brushes. One method that is not recommended by the author of the plugin is to hack up the existing plugin by adding brushes to the third-party directory and editing syntaxhighter.php file. As updates to the plug-in become available, your customized hacks will be overwritten. The other approach, is the recommended approach and is easy enough.

The approach is to write your own plugin to hook into the SyntaxHighlighter Evolved plugin. I found several sites that layout the code, but I couldn’t get that code to work. I should start that this was tested on WordPress 3.21 with SyntaxHighlighter Evolved version 2.1.364. The code needed a little modification that was borrowed from the SyntaxHighlighter Evolved plugin.

  • Before we write the code, go to the plugins directory of your WordPress installation. Create a new folder, name it whatever desired. In this case, how about a directory named syntaxhighlighter-brushes.
    wp-content/plugins/syntaxhighlighter-brushes
  • Available here is a fine directory of third-party created brushes. There you should find the desired brushes. For this example, the brush for BAT, CMD, or batch files is used. Download the file and name it shBrushBat.js. Copy this file to the directory that was created above.
  • Create another file, name it whatever, however for this example syntaxhighlighter-addons.php.
  • Copy and paste the following code into the file syntaxhighlighter-addons.php.
<?php
/*
Plugin Name: SyntaxHighlighter Evolved - Addons
Description:  Adds support for third party languages to SyntaxHighlighter Evolved.
Author: Paul Combs
Version: 1.0
Author URI: https://it.megocollector.com/
*/

// SyntaxHighlighter doesn't do anything until early in the "init" hook.
add_action('init','syntaxhighlighter_regscript');

// Tell SyntaxHighlighter about this new brush.
add_filter('syntaxhighlighter_brushes','syntaxhighlighter_addlang');

// Register the brush with WordPress.
function syntaxhighlighter_regscript() {
	wp_register_script('syntaxhighlighter-brush-bat', plugins_url('syntaxhighlighter-brushes/shBrushBat.js'), array('syntaxhighlighter-core'),'20130118');
	}
// Add alternative names for your brush.
function syntaxhighlighter_addlang($brushes) {
	$brushes['batch'] = 'bat';
	$brushes['bat'] = 'bat';
	$brushes['cmd'] = 'bat';

	return $brushes;
	}
?>
  • Save the syntaxhighlighter-addons.php file to the directory named above. Your plugin folder should look like this.

wp-content/plugins/syntaxhighlighter-brushes
wp-content/plugins/syntaxhighlighter-brushes/shBrushBat.js
wp-content/plugins/syntaxhighlighter-brushes/syntaxhighlighter-addons.php

  • Activate the plugin, done.

Use the brush like you would any other brush that is part of the SyntaxHighlighter Evolved plugin.

So, how is this code different from the others. It is in two places. One a minor adjustment in labels to leave room for multiple brushes in the same plugin. The other is in this part of the script for the plugins_url where the path was not hardcoded.

wp_register_script('syntaxhighlighter-brush-bat', plugins_url('shBrushBatch.js',__FILE__), array('syntaxhighlighter-core'),'20130118');

It was changed to this to get it working (for me).

wp_register_script('syntaxhighlighter-brush-bat', plugins_url('syntaxhighlighter-brushes/shBrushBat.js'), array('syntaxhighlighter-core'),'20130118');

Hope this helps someone else.

Source(s)
Adding A New Brush (Language) << Viper007Bond.com
Regex | Programming Thoughts
http://sysadmin-e.com/notes-syntaxhighlighter
http://www.undermyhat.org/blog/2009/09/list-of-brushes-syntaxhighligher/