code indentation in wordpress

If, like me, you are a developer and occasionally put snippets of code on your wordpress blog you will no dount have found out that the <code> tag strips out the indentation and makes the resulting post very difficult to read.

The solution is to use the <pre> tag which will keep the indentation.

An example showing both <code> and <pre>:

Using <code>:
<?php
$d = "labby.co.uk";

if($d == "code indentation example")
{
echo "Thank you for not bothering to look at my example code";
}
else
{
echo "Hey, someone actually followed my code logic";
}
?>

Using <pre>:

<?php
  $d = "labby.co.uk";

  if($d == "code indentation example")
  {
    echo "Thank you for not bothering to look at my example code";
  }
  else
  {
    echo "Hey, someone actually followed my code logic";
  }
?>

Using <pre> and <code> together:

<?php
  $d = "labby.co.uk";

  if($d == "code indentation example")
  {
    echo "Thank you for not bothering to look at my example code";
  }
  else
  {
    echo "Hey, someone actually followed my code logic";
  }
?>

Now I just have to through all my posts and comments and convert them manually :(

This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to code indentation in wordpress

  1. Hey Greg, I also use WordPress to post code snippets on my blog, http://scott.donnel.ly . I use the Easy Google Syntax Highlighter, available here: http://blog.burlock.org/wordpress/88-easy-google-syntax-highlighter-for-wordpress
    With this you can get much prettier and more readable results than just (or is it <pre> ?)

    Google Chrome 18.0.1025.168 Ubuntu 64 bits
    Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/12.04 Chromium/18.0.1025.168 Chrome/18.0.1025.168 Safari/535.19
    • Greg says:

      Thanks for the heads up Scott, I am perusing your blog as I type, it looks very interesting, full of the stuff I like.

      I will look at the highlighter and give it a try.

      Google Chrome 21.0.1180.81 GNU/Linux 64 bits
      Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.81 Safari/537.1

Leave a Reply

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

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

CommentLuv badge