Enabling syntax highlighting

Another UPDATE

This post is outdated. I am now using ‘Crayon Syntax Highligther’ instead of ‘Syntax Highlighter Evolved’. I had to update the code samples on this page accordingly, so reading this post might lead to confusion.


There is a newer version of the plugin that I wrote about in this post. It’s functionality is available by default to wordpress.com users. The new plugin is called SyntaxHighlighter Evolved.

SyntaxHighlighter Evolved is compatible with Syntax HighLighter for WordPress, my blog uses SyntaxHighlighter Evolved now. Everything in this post also applies to SyntaxHighlighter Evolved.

I would like to know what happens if I enter some php code. Does the syntax get highlighted by default in WordPress? Is it going to be stripped out? Will it be forbidden? Let me just type “echo ‘Hello World!'” and see what happens:

<?php echo ‘Hello World’; ?>

I expect this to show up just as I typed it, nothing special.

Now let me type it into the html editor, and see what I get then:

(You see nothing here, but on the backend, in the editor there is a hidden html comment that contains the php code. On the front-end, without the editor, the comment gets stripped out completely)


It gets commented out like this:

<!–?php echo ‘Hello World in the html editor’; ?–>

I remember there was a setting to allow php, or was that Drupal only? It must have been Drupal, because I can’t find it in WordPress.

In fact, it doesn’t matter. What I am after at the moment is not to have posts executed, I only want syntax highlighting.

I search for syntax highlighting in the plugins repository and I find six pages of plugins for it. One that looks very promising from its’ statistics is ‘Syntax Highlighter for WordPress’.
[Note, January 2016: I changed the plugin to ‘Crayon’] And this is what it does:

It just requires pseudotags around the code snippet.  To preserve indentation, the pseudotags themselves had to be wrapped in <pre></pre> tags, or you could turn off the visual editor for yourself (this is an option in WordPress). If you use the visual editor without <pre> tags all indentation would simply be lost. In newer versions of Syntax Hightligther, which I am using now, there is no longer a need to insert the <pre></pre> tags, in fact, you MUST NOT put them in yourselves, since this will break your page. The Syntax Highligther will now insert them for you and do it wrong if <pre></pre> tags are already in place. Note that you need to switch to html view to paste the code with the indention into the pseudotag. Be careful not to switch back and forward between HTML and Wysiwyg view, because the that may destroy the indentation again… 🙁

Author: Bart McLeod

Zend Framework MVC specialist. Also interested in PHP, Zend Framework in general, Three.js and VRML.

3 thoughts on “Enabling syntax highlighting”

  1. You can now put sourcecode in comments.

    Source code is placed using the pseudotags described above. The long form will also work, so you can use both [php][/php] and [sourcecode language=”php”][/sourcecode].

    Look at the plugin documentation at wordpress.com if you want to learn about options such as a highlighted line of code.

  2. Thanks for pointing that out! It’s obviously double escaped on this page, maybe a problem with switching in editors or so. I’ll look into it.

    Update: I solved it by pasting in some new sample code (from the new plugin). I could *not* reproduce the double escaping by switching for HTML editor to WYSIWYG and back.

Leave a Reply

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

Resolve this please: *