Code »Posting Source Code
While WordPress.com doesn’t allow you to use potentially dangerous code on your blog, there is a way to post source code for viewing. We have created a shortcode you can wrap around source code that preserves its formatting and even provides syntax highlighting for certain languages, like so:
To accomplish the above, just wrap your code in these tags:
your code here
lang) parameter controls how the code is syntax highlighted. The following languages are supported:
- matlab (keywords only)
If the language parameter is not set, it will default to “text” (no syntax highlighting).
Code in between the source code tags will automatically be encoded for display, you don’t need to worry about HTML entities or anything.
The shortcodes also accept a variety of configuration parameters that you may use to customize the output. All are completely optional.
autolinks(true/false) — Makes all URLs in your posted code clickable. Defaults to true.
collapse(true/false) — If true, the code box will be collapsed when the page loads, requiring the visitor to click to expand it. Good for large code posts. Defaults to false.
firstline(number) — Use this to change what number the line numbering starts at. It defaults to 1.
gutter(true/false) — If false, the line numbering on the left side will be hidden. Defaults to true.
highlight(comma-seperated list of numbers) — You can list the line numbers you want to be highlighted. For example “4,7,19”.
htmlscript(true/false) — If true, any HTML/XML in your code will be highlighted. This is useful when you are mixing code into HTML, such as PHP inside of HTML. Defaults to false and will only work with certain code languages.
light(true/false) — If true, the gutter (line numbering) and toolbar (see below) will be hidden. This is helpful when posting only one or two lines of code. Defaults to false.
padlinenumbers(true/false/integer) — Allows you to control the line number padding.
truewill result in automatic padding,
falsewill result in no padding, and entering a number will force a specific amount of padding.
title(string) — Set a label for your code block. Can be useful when combined with the
Here’s some examples of the above parameters in action:
And here’s a larger code block to see it all in action. It is set to the PHP language with “htmlscript” enabled and line number 12 highlighted.
Alex Gorbatchev’s SyntaxHighlighter project was used in the implementation of this feature. If you are a WordPress.org user and would like to use this feature on your self-hosted blog, it is available as a plugin.