How to Escape Shortcodes in When Posting Code Snippets in WordPress

If you're trying to post example shortcodes in your post but finding that the shortcode is getting processed, here's how to escape it and present the shortcode as plain text.

This is another one of those very simple solutions to a frustrating problem, but a solution that’s surprisingly difficult to find an answer to on the web. I’m posting it here not because it’s especially innovative, but simply because I had trouble finding the solution elsewhere and hopefully this will save someone else some time. And for my own future reference too, for that matter.

I was recently writing up a post about changing the “Filed Under” and “Tagged With” metadata labels for WordPress posts, and I needed to include shortcode snippets in the examples. The problem is, those shortcodes were being rendered even in text designated a code block. So I needed a way to exclude specific shortcodes in the text from being processed. Using a backslash, as you might in some coding languages and regex, doesn’t work.

This only applies to valid shortcodes that are being called on your WordPress installation. If it’s not a valid, installed shortcode like the nonsense one I’m using on this page as the example, processing it doesn’t do anything so it’s just treated as plain text.

There are plugins that will do it, along with other features. The Raw HTML is one I’ve used for other reasons in the past that has worked well. But I figured there had to be a way to escape the shortcode without installing yet another plugin, and especially a plugin that might have unintended consequences elsewhere on the site.

Convert Square Brackets to Unicode

In theory, converting the square brackets to unicode should work. You’d replace:

[ with [
] with ]

If you're looking to create banners, thumbnails, or other graphics for your social media accounts, a good option worth looking at is Canva. It runs in a web browser, which means nothing to install on a computer, and you can even use a mobile device. So you can do it from anywhere, and it's very straightforward to use. There's a very capable free plan, and you can get many more useful features, photos, and fonts with the Pro plan. The Pro plan has a 30-day free trial, which you can get here.

to create something like:

[shortcode_here]

But for whatever reason, that didn’t work for me. Nor did processing it with an HTML encoder. It might have had something to do with using it in a code block. So I’ve used the next option.

Double the Brackets

In the end, I found the solution in a ticket from 2007 in the WordPress core issue tracker. It was something baked into WordPress Core way back in version 2.5. And it’s very simple. Just double the brackets.

A shortcode with single brackets will be processed. By doubling the brackets, you’re escaping the square brackets.

So instead of typing:

[shortcode_here]

You’d type:

[[shortcode_here]]

Like I said, it’s really simple. But probably because not many people need it very often, it’s not widely publicized. But if you’re one of those handful of people who do need, hopefully this saves you some time and frustration.

More Tips & Tricks:

View Comments

Share