Display code in WordPress

WordPress v2.1When, as I do, you want to include some bits and pieces of software code in a WordPress post, it starts to become a serious headache. As a matter of fact, WP has not really been prepared for this and it creates a number of issues. Those I already encountered here:

  • The <?php tend to disappear (unless you start juggling)
  • Some charcaters would not display at all (or badly)
  • The overall presentation is pretty bland if you use <code>

So, I looked for a plugin (nearly everything in WordPress is done by adding on of those marvellous little code gems that can be included in a few seconds into the basic configuration) adapted to this task, easy to install (I don’t want to be stuck with heavy maintenance just for this), easy to use.

Here is the summary of what I checked and my opinion regarding them.

Code markup v1.1.1

Syntax

Just insert your code between <pre><code> and </code></pre>.

In my opinion

Pros:

  • Correctly avoids WordPress text transformation
  • It is still possible to add some manual formating in the code itself
  • Compatible with WordPress 1.5, 2.0 & 2.1
  • Single file plugin, to be activated from the WP dashboard

Cons:

  • No syntax coloration

ElasticDog Code viewer v1.1

Syntax

Your code is stored in an external file, is displayed with indentation and auto line-return, may be downloaded separately.

In my opinion

Pros:

  • Rather flexible presentation/format (e.g. line numbering)
  • External file storage allows code duplication if it appears in several posts or in several locations)
  • Single file plugin, to be activated from the WP dashboard; Plus described installation procedure

Cons:

  • External file storage: Rather heavy duty solution for small snipets of code

Coffe2code Preserve code Formatting v0.9

Syntax

Just insert your code between <pre><code> and </code></pre> (some other tags can also be defined).

In my opinion

Pros:

  • Correctly avoids WordPress text transformation
  • Single file plugin, to be activated from the WP dashboard

Cons:

  • Not updated since 2005. It is not even sure it was tested with WordPress v2.1 ?
  • No syntax coloration

Priyadi’s Code Autoescape v2.0

Syntax

Just insert your code between <pre><code> and </code></pre>.

In my opinion

Pros:

  • Correctly avoids WordPress text transformation
  • Compatible with WordPress 1.5, 2.0 & 2.1
  • Single file plugin, to be activated from the WP dashboard

Cons:

  • No syntax coloration

CoolCode

In my opinion

Pros:

  • Includes syntax coloration

Cons:

  • The web site is 99% in Chinese (I did not complete a real evaluation).

Dean Code Source Syntax highlighting v1.1

Syntax

Just insert your code between <pre lang=”php”> and </pre> (specifying PHP language or any other language).

In my opinion

Pros:

  • Correctly avoids WordPress text transformation
  • Compatible with WordPress 1.5+
  • Also includes syntax coloration (for many languages).
  • Single file plugin, to be activated from the WP dashboard, and a CSS file to fine-tune formating

Cons:

  • You must define CSS tags (minor problem since, for the others, you prefer to do so)

RobM Code entities v1.1

Syntax

In view of the current evolution of this type of plugins, even the author advises to use Priyadi’s Code Autoescape instead of his own.

Conclusion

As you can deduct from the information I provided here, my choice was Dean Code Source Syntax highlighting that I currently use for Roumazeilles.net. The strongest reason was clearly the ability to do syntax coloration to increase readability even for small code snippets (I care about my readers confort).

I can’t say I saw every aspect of these plugins in the comparison (benchmark) but it led me to a reasoned choice that I wanted to share with you. You may be doing a different choice; Feel free to tell us which one and why.

Examples

PHP code

Here is how PHP source code appears:


CSS code

Here is how a CSS file is displayed:

/*******************************/
/* Links                       */
/*******************************/
a:link    { color:#a0a010 }
a:visited { color:#606000 }
a:hover   { color:#e0e020; text-decoration: underline }
a:active  { color:#e0e020; text-decoration: underline }

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.