1

This is my data (example):

var obj = {
something:'custom'
people:[
    {
        name:'john',
        age:51
    },
    {
        name:'jenny',
        age:62
    }
]
}

And I use such shortcode:

[foo name="john,jenny" age="51,62" something="custom"]

Let say I need to have multiple objects like that in one shortcode, how would I make the shortcode look like, so I can process it and get data?

2
  • Are you trying to use the shortcode to get data from the object? Or put data into an object? What are you trying to achieve here? Dec 4, 2019 at 0:32
  • I am just wonering how the shortcode can be formatted, what is allowed?
    – Toniq
    Dec 4, 2019 at 1:25

1 Answer 1

0

you can make a string and parse it. you start with

[foo people="name:john,age:51|name:jenny,age:62" something="custom"]

and you use this code

add_shortcode("foo", function ($attr, $content, $tag) {


    // parsing attributes

    $attr["people"] = explode("|", $attr["people"]);

    $attr["people"] = array_map(function ($e) {

        $tab = [];

        foreach (explode(",", $e) as $raw_tab) {
            $tab2 = explode(":", $raw_tab);
            $tab[$tab2[0]] = $tab2[1];
        }


        return $tab;

    }, $attr["people"]);


    /*

    $attr = array(
        'people' => array(
            0 => array(
                'name' => 'john',
                'age' => '51',
            ),
            1 => array(
                'name' => 'jenny',
                'age' => '62',
            ),
        ),
        'something' => 'custom',
    );

    */


    // generate output

    $result = "...";


    return $result;

});
1
  • Why cant I nest shortcode like this? [foo][name="john,jenny" age="51,62" something="custom"][name="anna" age="22"][/foo] I would like to do something like this.
    – Toniq
    Dec 4, 2019 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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