How to post code in a wordpress blog so it maintains its formatting?

Hey all,

This was really easy to figure out, since there is a nice WordPress discussion about it here:
http://en.support.wordpress.com/code/

So a quick example:

So if you want your code to show up in your post and be nicely formatted and colored, similar to how it is in your favorite IDE, then put your code in between the following lines:

[sourcecode language=”cpp”]
int main()
{
    //Code
}
[/sourcecode]

It looks like this:

int main()
{
	//Code
}

Put your cursor over the code and check out the top right icons that appear. You can simply click an icon to open the source in a text editor or copy the source to your clipboard.

By the way, you must have the “language=” tag and you must use one of the following short names for the code language:

  • bash
  • cpp
  • csharp
  • css
  • delphi
  • html
  • java
  • jscript
  • php
  • python
  • ruby
  • shell
  • sql
  • vb
  • xml

One Comment

Leave a Reply

How to post code in comments?