How do I escape a ‘]’ in a short code?

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.

Read More

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?

Related posts

Leave a Reply

6 comments

  1. 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.

  2. 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]]

  3. 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.

  4. 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

  5. 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.

  6. 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]