Skip to content
Mar 11 / kkrizka

Code Syntax Highlighting In WordPress

screenshot-2.png

From time to time I post random code on this blog and I want it to be displayed in a legible manner. This is quite impossible with the default WordPress setup, because all code will just be displayed like random text. Luckily there is the wp-syntax plugin, which will detect the code and apply different styles automatically (using GeSHi) to it so it looks just like in your editor.

I recommend that you read the official wp-syntax usage notes, as they can simply your life by quite a lot. Here are a few notes of my own.

  • If your code contains both HTML and PHP, set the language to PHP (you can’t do both). It should also highlight the HTML tags.
  • Apply the styles suggested in the usage notes. They take care of text overflow problems and distinguishing post text from code.
  • Omit the line=”1″ argument if you do not want line numbering.
  • Do not replace < with < and so on. wp-syntax will do this conversion for you.

For now I’ll leave you with an example of wp-syntax in action:

for($i=0; $i&lt;100; $i++){
  echo "Hello world!";
}
Leave a comment