What a pain in the rear! WordPress appears nice as a blogging tool, but my attempts to format some code snippets was painful beyond belief.
Every time I pasted some code in and surrounded it with the code tags, the generated HTML had extra paragraph markers and line breaks, and screwed up the position of the closing code marker.
Even a search for plugins did not cure things. I installed one or two “code formatting” plugins that made things a little better, but still required a lot of post-paste editing of code to get anything like presentable.
It turns out that the Rich Text Editor in WordPress 2 is to blame – it completely messes up. So my advice is to disable this from your user profile.
The thing that rankles is that the rich editor is enabled by default. I did not even know it was optional until I did a Google on code formatting in WordPress. It all just seemed much much harder than it should.
Anyway, hopefully this snippet will save others the problem in the future.