Embedding code in WordPress posts and pages can be a fiddly process.
There is a page in the WordPress codex dedicated to Writing Code in Your Posts, which is a good place to start from.
As GitHub became more popular in the past two years, many leading WordPress developers have switched to Gists to display their code.
Gist is a simple way to share snippets and pastes with others. All gists are git repositories, so they are automatically versioned, forkable and usable as a git repository.
Bill Erickson moved all his code to Gists and blogged about it:
“Keeping WordPress from messing up code you’re sharing in the post editor can be difficult, and a lot of my old posts had mangled code. By moving everything to github I don’t have to worry about this anymore. It also means I can have a single source of code linked from multiple places (in a blog post, in a code snippet post…).”
Take a look at these two examples:
I’m still undecided whether to switch to Gists, my main concern is that I’d like to keep full ownership, although GitHub is unlikely to be going anywhere in the next few years, considering how popular its become.
I think at the next instance of finding a post with mangled post on my site, I will take the plunge.
Inserting Shortcodes in Documentation
If you have are preparing some documentation and want to insert shortcodes, but not have them run within your post or page, you’ll probably start looking for a plugin to save your day.
It happened to me and I found these two excellent plugins:
The only alternative is to use a full blown syntax highlighter from the plugins mentioned earlier, but that’s overkill in my opinion.
You can also replace ‘[‘ and ‘]’ with their HTML character entities, but that’s a massive pain in the backside.
If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.