Making your code look pretty in WordPress posts
April 5, 2009
Since the very first time when I started this blog I have been trying to find a better way of formatting my code. I am using Windows Live Writer to write and post my blogs and it has a lot of good plug-ins to not only format your code but many other things such as inserting pictures, videos, maps, and many many more. However I was not able to find a good plug-in to format my code and I started digging around. I am partially writing this post so I can later use this post as a reference for my self and others if they are interested.
What I have found out is that WordPress is already supporting a great code formatter tool that was written by Alex Gorbatchev called Syntaxhighlighter.
All you have to do in order to format your code is to enclose it into a sourcecode block and the wordpress engine will take care of formatting your code based on the language that you specified. Here is a simple example of what a piece of ruby code might look like:
[ sourcecode language=’ruby’]
def class BeautifulCode
def display_beautiful_code
puts “SyntaxHighlither rocks!!”
end
end
[ /sourcecode]
The output after formatting will look like this.
def class BeautifulCode def display_beautiful_code puts "SyntaxHighlither rocks!!" end end
Just make sure to remove the spaces that I have intentionally included in the first source code sample because WordPress would otherwise try to display the formatter version instead of the actual code that I was trying to display.