Shortcodes play a vital role in WordPress, and using shortcodes in your posts or pages is super easy. But sometimes, you don’t want your shortcodes to be executed, especially when you are writing something about shortcodes or maybe plugin review or document. In that case, we usually escape the shortcodes.
Escaping shortcodes in WordPress is not rocket science. You can escape any shortcodes by surrounding it with double square brackets. Let’s say you have a shortcode [testimonials], that will display testimonials but you want to display it as it is.
So you just need to surround your shortcode with an extra set of square brackets. Let’s see the below examples:
Shortcodes without attributes
Shortcodes that have no attributes can be escaped by putting it within double square brackets, something like:
Shortcodes with attributes
Same as above if you have shortcodes that consist attributes, you can surround that too with another set of square brackets.
[[testimonials item="3" color="#f00" ]]
Shortcodes with Start and End Tag
If you have a shortcode that has a start and an end tag, then you can escape it by putting both start tag and end tag within a set of square brackets.
Let’s say you have a shortcode
[testimonials] some text to show [/testimonials]
Then you can simply put it within square brackets something like
[[testimonials] some text to show [/testimonials]]
This is the correct, safe and easy way to escape the shortcodes in WordPress.
I hope this article helps you to understand, How to escape shortcodes in WordPress and display them in your posts and pages as code snippets.
Remember: You can only escape shortcodes that are registered with your WordPress. If you are trying to escape a text with double square brackets, it will not work if that text is not registered as a shortcode.