15

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?

16

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 '11 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. – Johannes Pille Nov 17 '11 at 17:55
  • @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 '13 at 6:31
2

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.

1

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:

[gallery]

You would write this:

[[gallery]]

0

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 :

 $origvalue=urldecode($atts['myattr']);

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.

Pierre

-1

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]

  • 3
    The question is about inserting ]as part of the content, not about how to display it. – Mark Kaplun Apr 12 '16 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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