WP-Syntax provides clean syntax highlighting using GeSHi — supporting a wide range of popular languages. It supports highlighting with or without line numbers and maintains formatting while copying snippets of code from the browser.
It avoids conflicts with other 3rd party plugins by running an early pre-filter and a late post-filter that substitutes and pulls the code snippets out first and then pushes them back in with highlighting at the end. The result is source code formatted and highlighted the way you intended.
Usage
Wrap code blocks with <pre lang="LANGUAGE" line="1">
and </pre>
where“LANGUAGE” is a GeSHi supported language syntax. See below for a full list of supported languages. The line
attribute is optional.
Supported Languages
The following languages are most supported in the lang
attribute:
abap, actionscript, actionscript3, ada, apache, applescript, apt_sources, asm, asp, autoit, avisynth, bash, bf, bibtex, blitzbasic, bnf, boo, c, c_mac, caddcl, cadlisp, cil, cfdg, cfm, cmake, cobol, cpp-qt, cpp, csharp, css, d, dcs, delphi, diff, div, dos, dot, eiffel, email, erlang, fo, fortran, freebasic, genero, gettext, glsl, gml, bnuplot, groovy, haskell, hq9plus, html4strict, idl, ini, inno, intercal, io, java, java5, javascript, kixtart, klonec, klonecpp, latex, lisp, locobasic, lolcode lotusformulas, lotusscript, lscript, lsl2, lua, m68k, make, matlab, mirc, modula3, mpasm, mxml, mysql, nsis, oberon2, objc, ocaml-brief, ocaml, oobas, oracle11, oracle8, pascal, per, pic16, pixelbender, perl, php-brief, php, plsql, povray, powershell, progress, prolog, properties, providex,python, qbasic, rails, rebol, reg, robots, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, tcl, teraterm, text, thinbasic, tsql, typoscript, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xml, xorg_conf, xpp, z80
See the GeSHi Documentation for a full list of supported languages.
(Bold languages just highlight the more popular ones.)
Example 1: PHP, no line numbers
12345678910 <pre lang="php"><div id="foo"><?phpfunction foo() {echo "Hello World!\\n";}?></div></pre>
1 2 3 4 5 6 7 |
<div id="foo"> <?php function foo() { echo "Hello World!\\n"; } ?> </div> |
Example 2: Java, with line numbers
12345678 <pre lang="java" line="1">public class Hello {public static void main(String[] args) {System.out.println("Hello World!");}}</pre>
1 2 3 4 5 |
public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); } } |
Example 3: Ruby, with line numbers starting at 18
12345678 <pre lang="ruby" line="18">class Exampledef example(arg1)return "Hello: " + arg1.to_sendend</pre>
1 2 3 4 5 |
class Example def example(arg1) return "Hello: " + arg1.to_s end end |
Example 4: If your code already has html entities escaped, use escaped="true"
as an option
1234 <pre lang="xml" escaped="true">&lt;xml&gt;Hello&lt;/xml&gt;</pre>
1 |
<xml>Hello</xml> |
Example 5: PHP, with line numbers and highlighting a specific line
12345678910 <pre lang="php" line="1" highlight="3"><div id="foo"><?phpfunction foo() {echo "Hello World!\\n";}?></div></pre>
1 2 3 4 5 6 7 |
<div id="foo"> <?php function foo() { echo "Hello World!\\n"; } ?> </div> |
Example 6: PHP, with a caption (file and/or file path of the source file)
123456789 <pre lang="php" src"https://github.com/shazahm1/Connections/blob/master/connections.php"><div id="foo"><?phpfunction foo() {echo "Hello World!\\n";}?></div></pre>
Styling Guidelines
WP-Syntax colors code using the default GeSHi colors. It also uses inline styling to make sure that code highlights still work in RSS feeds. It uses a default wp-syntax.css
stylesheet for basic layout. To customize your styling, copy the default wp-content/plugins/wp-syntax/wp-syntax.css
to your theme’s template directory and modify it. If a file named wp-syntax.css
exists in your theme’s template directory, this stylesheet is used instead of the default. This allows theme authors to add their own customizations as they see fit.
Advanced Customization
WP-Syntax supports a wp_syntax_init_geshi
action hook to customize GeSHi initialization settings. Blog owners can handle the hook in a hand-made plugin or somewhere else like this:
1 2 3 4 5 6 7 8 9 10 |
<?php add_action('wp_syntax_init_geshi', 'my_custom_geshi_styles'); function my_custom_geshi_styles(&$geshi) { $geshi->set_brackets_style('color: #000;'); $geshi->set_keyword_group_style(1, 'color: #22f;'); } ?> |
This allows for a great possibility of different customizations. Be sure to review the