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.
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:
to create something like:
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:
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.