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.

Last Updated:

This post may include affiliate links. Read more.

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 ]

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]]

With Opening and Closing Shortcodes

If you’re doing a line with opening and closing shortcodes, you’d only double-bracket the first and last instances, like this:

[[shortcode_here]TEXT HERE.[/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 it, hopefully this saves you some time and frustration.

David Coleman / Photographer

David Coleman

I'm a professional freelance travel photographer based in Washington DC. Seven continents, up mountains, underwater, and a bunch of places in between. My images have appeared in numerous publications, and you can check out some of my travel photography here. More »