John LeschinskiPlugin: Syntax Highlight.

Written by John Leschinski from Leschinski Design on April 26, 2008

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>
[/sourcecode]

Will displays like this.

< ?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>

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.

Written by John Leschinski from Leschinski Design on April 26, 2008 | Filed Under WordPress Plugins

Share with others

  • StumbleUpon
  • Add to Delicious
  • Mixx
Unique Blog Designs

2 Responses so far | Have Your Say!

  1. Sarah  |  April 26th, 2008 at 5:45 pm #

    Sarah - Gravatar

    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!

  2. superjason  |  April 26th, 2008 at 5:58 pm #

    superjason - Gravatar

    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)

Trackbacks to 'Plugin: Syntax Highlight.'

Leave Feedback

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>