How do I escape a right bracket in a short code?

I'm working on a Google Map plugin that has encoded points. Sometimes the polygons have the ] in it, which makes WP think that it's the end of the shortcode.

For example:

[my_shortcode latitude='36.93'  longitude='-72.98' encoded_points='ortlF~g]tM?cZEH`z]}|@DQfi]' ]

I've tried ] which is isn't causing a problem for WP, but it is causing a problem for my Google map code. I could use Regex to replace ], but maybe there is a simpler way. Does shortcode have an escape character?

6 Answers 6


I don't know of an official escape syntax for shortcodes and there likely isn't one.

When wordpress parses for shortcodes it looks for [ and ]. If you want to use square brackets within a shortcode, using the respective html ASCII entities escapes them.

I.e. replacing [ by [ and ] by ]. Wordpress will not recognize ] as the end of the shortcode.

Whether that serves your purpose obviously depends on whether it gets converted to ] before being passed to the Google Maps API or whether the API handles it as expected. I have no experience with that, so can't say.

  • Thank you for you answer. In my question, I forgot to put ']' in a code block so it came out as a ']'. I've edited my question. The HTML entity works for WP, but not for Google Maps. I'll have to use Regex to replace the HTML entities. Thank you very much for the help.
    – Laxmidi
    Nov 17, 2011 at 17:47
  • @Laxmidi My above "guess" that there is no escape character borders on certainty - just wanted to be cautious to not provide misinformation. If there was, I'm positive the codex article on the Shortcode API would mention it. I think replacing afterwards is your best bet. Do you need a regex for one character though? I think str_replace() should get the job done as well. Would be less resource intensive, if it did. Nov 17, 2011 at 17:55
  • 1
    @JohannesPille hello it's an oldie, but i need to have the [ ] also replaced by ]. First time saving in WP and it works, but when editing the post/page WP turns ] into ]. (need this for a google event tracking on shortcode buttons) regards
    – alex
    Oct 18, 2013 at 6:31
  • As @alex said, if you switch to visual tab and return to code tab will see that tinyMCE changes the special characters back to [ ], then breaking the code. Nov 9, 2019 at 2:43
  • tinyMCE is Javascript and pluggable. Take a look at wpse/questions/223677/adding-custom-text-patterns-in-the-wp-4-5-visual-editor. @alex Nov 10, 2019 at 3:10

There seems to be an official page here : Escaping Shortcodes

Extract :

To do this, you need to escape the shortcode by using two sets of brackets instead of just one. So to display this in your post:


You would write this:


  • Yes but no. From the same source: Note that only shortcodes that are currently available can be escaped in this manner. Using double brackets around text that is not currently registered as a shortcode will result in both sets of brackets being displayed in your post, not just one as when escaping registered shortcodes. Nov 22, 2021 at 15:20

Based on the Johannes Pille's answer there is wp function to escape square brackets for using text in shortcode parameters:

function my_esc_brackets($text = ''){
    return str_replace( [ "[" , "]" ] , [ "[" , "]" ] , $text );

Often people suggest using shortcode's $content for text parameters, but there are cases when the shortcode has many such text parameters.


Even if you are NOT playing with url, use urldecode(your attribute_value) with %5B and %5D used as replacement for [ and ] in your attribute_value.

Your code may then look like this :

In your wp page : [my_shortcode myattr="%5Bmyattribute_value%5D"]

Then, in your shortcode function, just do this :


This will restore the [ and ] characters in the $origvalue, so that $origvalue now contains : [myattribute_value]

TIP : If you are playing with url, do the same, but add the following to the function code :

$urlvalue = urlencode($origvalue);

If that is not clear : YES, I am doing urlencode(urldecode(some value from $atts)) to build a correct part of an url.

Quite simple, hope this helps.



The easiest solution - if only one attribute needs escaping is to use shortcodes in the open / close tag version like so:

[my_shortcode latitude='36.93'  longitude='-72.98']ortlF~g]tM?cZEH`z]}|@DQfi][/my_shortcode]

then you get your problematic content as the second argument $content (the first is $attr).

You can check the documentation on that to get more info.


This is an old question, but here's a workaround using css. In place of braces, enclose characters in a span, like so:

<span class="bracket">some string</span>

then, add this to the theme stylesheet:

span.bracket:before {
  content: '['
.colored-blocks span.bracket:after {
  content: ']'

the result will look like this:

[some string]

  • 4
    The question is about inserting ]as part of the content, not about how to display it. Apr 12, 2016 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.