Getting source code to display correctly in your blog can be a pain. I tried and tried to work with the <code> tags, but occasionally the visible code wasn't ok (for example replacing quotes and doublequotes incorrectly), taking one "-" off from html comment etc. Jumping on and off from "Visual" and "HTML" editor and updating a post seems to do this.
But of course there's a solution in the wonderful open source community and WordPress plugins that utilize the power. This isn't exactly breaking news as these have been around for while, as I picked up this post:
- WordPress Support: Writing & Editing Code. At that time, Alex Gorbatchev’s SyntaxHighlighter Google Code project was used to implement the source code highlighting feature.
I checked if this would've worked by default on self-hosted WordPress, but it seemed like it wouldn't, so I went to plugin hunt. And there seemed to be aplenty of plugins for source code publishing, some with syntax highlighting, some just making sure code displays correctly in <code> tag.
But this is what I decided to try for now and it seems to work nicely:
So whether you want to display just some html code or more advanced source code, these kind of plugins are for you. They are good for both one-two liners as they are for hundreds of lines of serious code. There's plenty of tags in use and all you have to do is enclose the code in a tag and define a language if you want. And the plugin supports a "lighter" style with no highlighting, but securing the formatting the same way.
Since writing this post I have removed the plugin, as I only post sourcecode to posts so often, and the plugin is on with the default so it puts the javascript code to every page, unless you set if off on the post window. However, if you're blog is about coding and you post sourcecode, like html, php, c, or whatever almost every post, this plugin works great (at least until WordPress version 2.7).
Thanks for all these great blogs and bloggers for helping me by providing a bunch of information on the subject:
- WordPress Plugins That Help You Write Code @Lorelle on WordPress
- Writing Code in Your Posts - WordPress Codex
- Posting Source Code @WordPress.com Blog
- Writing and Publishing Code in Your WordPress Blog Posts @The Blog Herald
- Displaying Code In WordPress Posts @ThemeLab
So if you like to read more about source code in posts, check the above for detailed info.