Syntax highlighting in WordPress

I wanted to add syntax highlighting of all my code examples, and hoped there was a theme that did just that. Nope :cry:

But then I found SyntaxHighlighter by Alex Gorbatchev! This is a beautifull syntax highlighter written in JavaScript that supports various languages. Now the only problem is to integrate it with WordPress, but fortunately somebody else thit the work and made the Auto SyntaxHighlighter plug-in.

So after installing this plug-in I updated one of my old post an wrote:

<pre class="brush:java">
class MyClass {
private int n = 0;
public static final const String VALUE = "something";

public MyClass()
{
}

public int main(string argv[])
{
}
}
</pre>

But nothing happened! I found that the Journalist theme that I use (like many others) forgets to call wp_footer(); in footer.php, so adding this call in the theme finally solved the problem!

The plug-in works flawlessly, but is missing some features like highlighting and copying the contents in one click. The SyntaxHighlighter Evolved plug-in is yet another plug-in that is a bit more awkward to use but has all the features present in the original SyntaxHighlighter script. Futhermore it’s easier to use: just put the code in between [<language>] and [/<language>], where language is one of an impressive list (I personally use bash, powershell, java, cpp, and xml). You may also add options like:

syntaxHighlighter options
Option Description
autolinks=false Don’t make URLs in the code clickable.
collapse=true Collapse the code. Useful when you have a long code that would distract form the rest of the article.
firstline=n Number of the first line, defaults to 1.
gutter=false No line numbers
highlight=n,m,… Highlight lines at the given positions.
htmlscript=true Highlight HTML in your code. Useful for php and javascript.
light=true Removes gutter and toolbar. Useful for single line code.
padlinenumbers=false Don’t pad line numbers.
toolbar=false Don’t display the toolbar.
wraplines=true Wrap long lines.
This entry was posted in Blogging, Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>