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?
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.
There seems to be an official page here : Escaping Shortcodes
Extract :
Based on the Johannes Pille’s answer there is wp function to escape square brackets for using text in shortcode parameters:
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 :
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
The easiest solution – if only one attribute needs escaping is to use shortcodes in the open / close tag version like so:
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:
the result will look like this:
[some string]