Adding A New Brush (Language)

While SyntaxHighlighter Evolved comes bundled with brushes (the “plugins” that add support for specific languages) for most of the popular programming languages, there still may be a language that isn’t supported but that you need.

You’ll want to start by checking out this awesome blog post (dead link). It has a list of a ton of third part brushes and links to download them. If your language isn’t supported, then check out the official documentation for how to write your own brush.

Once you have your brush that you want to add to my plugin, you’ll need to throw together a tiny little plugin of your own that will tell my plugin about the new brush so that it can be used.

We start by registering the Javascript file with WordPress (note we’re registering it, not enqueueing it as my plugin will do that itself if it’s needed):

wp_register_script( 'syntaxhighlighter-brush-language', plugins_url( 'shBrushLanguage.js', __FILE__ ), array('syntaxhighlighter-core'), '1.2.3' );

wp_register_script() takes the same parameters as wp_enqueue_script().

Some notes on the parameters:

  1. This needs to be in this syntax: syntaxhighlighter-brush-some-unique-language-stub
  2. This is the URL to the brush file. It’s filename and location don’t matter (I would advise not storing it in my plugin’s folder as it will be deleted when upgrading my plugin). I’d recommend using the cool plugins_url() function (unofficial documentation).
  3. The brush requires the core SyntaxHighlighter file, so list it as a dependency.
  4. An arbitrary version number that you should change if you modify the file (to break browser caches).

Okay, so now that the brush file is registered with WordPress, we need to tell SyntaxHighlighter about the new brush and what language aliases it can be used for. To do this, we use the “syntaxhighlighter_brushes” filter, like so:

add_filter( 'syntaxhighlighter_brushes', 'a_unique_function_name_of_your_choosing' );

function a_unique_function_name_of_your_choosing( $brushes ) {
	$brushes['alias1'] = 'language';
	$brushes['alias2'] = 'language';

	return $brushes;
}
  • “alias1” and “alias2” are some example aliases. These will be available directly as shortcodes or as valid parameters for the “language” attribute of my plugin’s shortcode.
  • “language” is the stub that you used above when registering the brush with WordPress.

Activate the plugin and you’ll be good to go!

Overall Example

Assuming you have a brush file called “shBrushFoobar.js” for the language “foobar” and that brush file is in the same folder as the plugin, here’s what the plugin could look like:

< ?php
/*
Plugin Name: SyntaxHighlighter Evolved: Foobar Brush
Description: Adds support for the Foobar language to the SyntaxHighlighter Evolved plugin.
Author: Your Name Here
Version: 1.0.0
Author URI: http://yourblog.com/
*/

// SyntaxHighlighter Evolved doesn't do anything until early in the "init" hook, so best to wait until after that
add_action( 'init', 'syntaxhighlighter_foobar_regscript' );

// Tell SyntaxHighlighter Evolved about this new language/brush
add_filter( 'syntaxhighlighter_brushes', 'syntaxhighlighter_foobar_addlang' );

// Register the brush file with WordPress
function syntaxhighlighter_foobar_regscript() {
	wp_register_script( 'syntaxhighlighter-brush-foobar', plugins_url( 'shBrushFoobar.js', __FILE__ ), array('syntaxhighlighter-core'), '1.2.3' );
}

// Filter SyntaxHighlighter Evolved's language array
function syntaxhighlighter_foobar_addlang( $brushes ) {
	$brushes['foobar'] = 'foobar';
	$brushes['fb'] = 'foobar';

	return $brushes;
}

?>

You can then use it like this:

[foobar]your code here[/foobar]

Or like this:


					

70 thoughts on “Adding A New Brush (Language)

  1. Pingback: WordPress Syntax Highlighting for YAML | tedious ramblings

  2. Pingback: carlynorama | SyntaxHighlighting Evolved WordPress Plugin and a Custom Arduino Brush

    • Spaces shouldn’t be touched inside of my shortcode. However when it renders with the syntax highlighting and stuff, the JavaScript package (which I am not the author of) removes excess indents if all lines have them. For example if every line had an extra tab at the beginning of it (such as being copied out of the inside of a function), it’d remove it to that the root line was left aligned. Proper alignment overall is kept however.

  3. Pingback: SyntaxHighlighter Evolved Brush for other languages – mikrom

  4. Pingback: Wordpress????????????????? | ?????

  5. Struggling with latest plugin & custom brush in WP5/Gutenburg. Code language doesn’t appear in Settings. Newbie here but was successful prior to WP5. Any changes needed to custom brush?

  6. It seems this post is a bit out of date with the latest version(s) of the plug-in. If possible it might be good to update this for others. I borrowed code from other custom brush plug-ins.

Comments are closed.