After years of publishing snippets of HTML and PHP code in this blog, I’ve yet to find an elegant way to represent the code when published- there’s a bit of a trick, since you don’t want the HTML or PHP to do what it normally does.
For a few years I have used the Preserve Code Formatting plugin, since it allows me to wrap all the code in <pre>…</pre> tags, and it honors spaces and does not process the code as source code.
Yet it’s not all that readable, and I’ve had to play tricks to get it to not run off the right side of the page.
I just installed it and tested it out on the last post that had code — on The Blogless Blog.
First of all, it can apply language specific formatting — it formats it beautifully, manages line wraps, and more.
To use this, you wrap code in a “shortcode” like:
code stuff here
So in writing a post, you can simply copy and paste the code right inside those tags
which than gets rendered as:
You get line numbering, syntax coloring, and the little buttons on the top right make it easy ti print the code chunk, or better, it can display it in a pop up window as plain text, so you can copy it easily:
This is definitely an improvement on the way I’ve been publishing code– I’m unsure how much back updates I will do, but plan to use this method going forward.
If your coding isn’t pretty (like mine), at least it can look pretty.