Syntax highlighting code snippets in WordPress
March 6th, 2009
My last post included some code snippets.
I’ve never found an altogether satisfactory way to include nicely formatted and syntax highlighted code in WordPress. I’ve tried various methods but always ended up settling on something so-so after spending a while on it and eventually deciding I was wasting my time.
But this time round I tried again and found Chris Cagle’s Definitive Guide on WordPress Syntax Highligher Plugins.
In the spirit of “if it’s good enough for him, it’s good enough for me” I decided to use the same plugin Chris settled on: Chili. You can see the results for yourself. However, although I’m fairly pleased with the end product, the process for getting there (using Live Writer) isn’t ideal. I’m documenting it here because, fortunately, I don’t often use code snippets, so this will serve to remind me how I did it:
- Copy the code you want to use.
- Paste it into Postable (an online processor for making code browser friendly):
http://www.elliotswan.com/postable/ - Click make it friendly.
- Copy the browser-friendly code.
- In Live Writer, go to Source view.
-
Create nested elements as follows:
<p><pre><code class="html"></code></pre></p> - Change "html" for "php" or "c#" or "mysql" or "javascript" or "css". There are a few other supported languages listed on the Chili site.
-
Paste the code into the middle, between the code tags. For example:
<p><pre> <code class="html"><p><pre><code class="html"></code></pre></p></code> </pre></p> - Publish your post.
Potentially similar posts
- Scott Hanselman and Chris Sells on managing people and your time – February 2010
- How to apply syntax highlighting to code snippets in MadCap Flare – June 2009
- Live Writer plug-in for twitterers – February 2009
- Viewing dynamically generated HTML in a help topic – January 2009
- WordPress anguish – September 2008