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.

But no more, I found what I hope is The One Code Plugin– SyntaxHighlighter Plus which is a WordPress implementation of the SyntaxHighlighter package for formatting JavaScript code.

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:

[sourcecode language=’php’]
    code stuff here
[/sourcecode]

So in writing a post, you can simply copy and paste the code right inside those tags

sourcecode shortcode

which than gets rendered as:

source-code display

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:

source-code-pop

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.

Learn more on WordPress Source Code Posting or see the original SyntaxHighter code which can be used to render source code on any web page.

If your coding isn’t pretty (like mine), at least it can look pretty.

If this kind of stuff has value, please support me by tossing a one time PayPal kibble or monthly on Patreon
Become a patron at Patreon!
Profile Picture for CogDog The Blog
An early 90s builder of web stuff and blogging Alan Levine barks at CogDogBlog.com on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person. And he is 100% into the Fediverse (or tells himself so) Tooting as @cogdog@cosocial.ca

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *