admin管理员组文章数量:1289891
I need to pass complex json with nested array of object as shortcode parameter.
For example I need to pass something like this:
{
"a":"va",
"b":{
"b1":"vb1",
"b2":[
{
"kb2":"vb2"
}
]
}
}
So I tried to set the shortcode in this way:
[my_shortcode parameters='{"a": "va", "b": {"b1": "vb1", "b2": [{"kb2": "vb2"}]}}']
The problem seems to be related to array (square brackets) indeed this other example works:
[my_shortcode parameters='{"a": "va", "b": {"b1": "vb1"}}']
Here is how parameters are handled in shorcode:
function myshortcodehandler($atts){
$arrparameters = json_decode($atts["parameters"], true);
ob_start();
?>
<pre><?php echo json_encode($arrparameters); ?></pre>
<?php
return ob_get_clean();
}
If json includes some nested array the output show only some brackets. So, what is the right way to pass complex json as shortcode parameter?
I've already red this documentation:
Thanks in advance.
I need to pass complex json with nested array of object as shortcode parameter.
For example I need to pass something like this:
{
"a":"va",
"b":{
"b1":"vb1",
"b2":[
{
"kb2":"vb2"
}
]
}
}
So I tried to set the shortcode in this way:
[my_shortcode parameters='{"a": "va", "b": {"b1": "vb1", "b2": [{"kb2": "vb2"}]}}']
The problem seems to be related to array (square brackets) indeed this other example works:
[my_shortcode parameters='{"a": "va", "b": {"b1": "vb1"}}']
Here is how parameters are handled in shorcode:
function myshortcodehandler($atts){
$arrparameters = json_decode($atts["parameters"], true);
ob_start();
?>
<pre><?php echo json_encode($arrparameters); ?></pre>
<?php
return ob_get_clean();
}
If json includes some nested array the output show only some brackets. So, what is the right way to pass complex json as shortcode parameter?
I've already red this documentation: https://codex.wordpress/Shortcode_API#Square_Brackets
Thanks in advance.
Share Improve this question edited Jul 14, 2021 at 9:45 assistbss asked Jul 14, 2021 at 9:39 assistbssassistbss 1536 bronze badges 3- 1 This seems like you're really stretching what shortcodes are supposed to be used for. Why do you need such a complex parameter? Are users going to be typing these values in? – Jacob Peattie Commented Jul 14, 2021 at 10:02
- the solution is really quite dependent on how this is being implemented - where does the JSON come from? – Paul G. Commented Jul 14, 2021 at 10:21
- The shortcode needs complex configuration and will be generated automatically. Users will just copy and paste the shortcode where they want. – assistbss Commented Jul 14, 2021 at 10:40
1 Answer
Reset to default 1Maybe there is a better solution, but serialize seems to do the job.
Instead of json I could pass json_decoded and then serialized parameters.
For example
$jsonval = json_decode('{"foo": "bar"}');
$serialized = serialize($jsonval);
Shortcode will be something like:
[myshortcode parameters='O:8:"stdClass":1:{s:8:"foo";s:3:"bar";}']
At this point I can handle parameters unserializing them:
function myshortcodehandler($atts){
$arrparameters = (array) unserialize($atts["parameters"]);
...
Seems to work also with array in json.
本文标签: pass complex json as shortcode parameter
版权声明:本文标题:pass complex json as shortcode parameter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741443393a2379048.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论