Plugin: Syntax Highlight.

By John Leschinski | WordPress Plugins

Apr 26

If you’ve ever tried to include a snippet of code in your post you might have run into a common problem where WordPress decides to work it’s magic and format your code into something pretty unusable for demonstration purposes. You might have tried to code tag, but that doesn’t help much either.

One solution is to run your post through a tool to convert the code into something WordPress won’t modify. This is what I used in my last post. But a better solution is a plugin called Syntax Highlighter that saves you the extra steps and formats everything much more nicely.

All you do is wrap your code in [ sourcecode language=’ Appropriate language here ‘] and voila. So the following,

[ sourcecode language='php']

<?php if ( is_category(5) ) { ?>

<a href="/News/"><div class="menu_active">News</div></a>

<?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=5&title_li='); ?>

<a href="/opinion/"><div class="menu_inactive">Opinion</div></a>
<a href="/archives/"><div class="menu_inactive">Archive</div></a>

Will displays like this.

[sourcecode language=’php’]< ?php if ( is_category(5) ) { ?>

< ?php wp_list_categories(’orderby=id&show_count=1&use_desc_for_title=0&child_of=5&title_li=’); ?>


Now isn’t this much nicer? Kevin just installed this plugin here at Blogging Tips and hopefully it will make it easier and prettier for posts with code from here on.

About the Author

John Leschinski is the creative director and founder of Leschinski Design. John's skill and talent is sought after by large and small companies and organizations, both local to the Rainy River district, London Ontario, and globaly. His experience and knowledge are also used in classrooms to groom the next generation of web developers and business seminars discussing e-commerce and the value of good design.

Sarah April 26, 2008

Glad to see they've upgraded this plugin to use the pre tag instead of a textarea like it used to.

Now, can someone explain why WordPress has a habit of putting a gap between the less than diamond bracket and the question mark for an opening PHP tag? I'm glad to know I'm not the only one that suffers from it!

superjason April 26, 2008

Grrrr. I wrote lengthy reply, but forgot the spam protection math. When you click "back", it forgets your whole message!

Anyway, this solution works good unless you post from Windows live writer. WLW doesn't like it if you have unencoded text in your post. I ended up creating a live writer add-in that encodes the text and puts it in the necessary "pre" tag.

I'll post a link to my blog post when I release the add-in.

How are you posting to your blog, is it with the WordPress admin post editor?

–SuperJason (my tech blog)

Comments are closed