Shortcode Bracket Attribute

The Shortcode API states that you cannot have square brackets in your attribute. So the following will not work:

[tag attribute="[Some value]"] 

In my case, a square bracket is required. What would be the best solution to the problem? I already tried to avoid the content in my shortcode function, but no luck.

I am using WordPress 3.3.1.

+4
source share
3 answers

Use another special character in your short code and replace it with square brackets in your short code function. β€œSince this is not what you want, here is an alternative.”

This seems to be the only thing I can think of that it will work in your case, instead of relying on the Shortcode API, you can use " apply_filters " in the content and then use preg_replace to write your own short message processing function .

0
source

If parentheses appear as part of the generated HTML, try using &#...; or as part of the url, use %...

Otherwise, if it concerns your own short code, just replace another character, for example. {} on [] , inside the short code.

If this is not your own shortcode, you can change the plugin / kernel. I would write shell code so as not to interrupt updates.

0
source

We save json in shortcode attributes. We decided to use base64_encode to hide the square brackets, but we ran into some problems:

  • Themecheck complains about base64 functions
  • If you want to make a replacement in the database, for example. to replace all old site URLs with the new site URL, the regex cannot see what's inside the base64 encoded string

There is another solution using htmlentities

 function encode($str) { $str = htmlentities($str, ENT_QUOTES, 'UTF-8'); // http://www.degraeve.com/reference/specialcharacters.php $special = array( '[' => '[', ']' => ']', ); $str = str_replace(array_keys($special), array_values($special), $str); return $str; } function decode($str) { return html_entity_decode($str, ENT_QUOTES, 'UTF-8'); } $original = '[1,2,3,"&",{a:1,b:2,"c":""}]'; $encoded = encode($original); $decoded = decode($encoded); echo "Original:\t", $original, PHP_EOL; echo "Shortcode:\t", '[hi abc="'. $encoded .'"]', PHP_EOL; echo "Decoded:\t", $decoded, PHP_EOL; echo "Equal:\t\t", ($original === $decoded) ? 'YES' : 'NO'; 

Output

 Original: [1,2,3,"&",{a:1,b:2,"c":""}] Shortcode: [hi abc="[1,2,3,"&",{a:1,b:2,"c":""}]"] Decoded: [1,2,3,"&",{a:1,b:2,"c":""}] Equal: YES 

http://ideone.com/fNiOkD

0
source

Source: https://habr.com/ru/post/1392540/


All Articles