How can i put an array as variable in shortcode_atts?

I am trying to create a shortcode with an array as input like so

function product_gallery_shortcode($atts) {

extract(shortcode_atts(array(
            'product_id' => '31',
            'prodvid' => false,
            'youtubeids'=>'',//['lbRqMddP2jo','eFAxx817rC0'],
            'thumbnr' =>2
                ), $atts));

I like to loop throught the youtube id’s but i don’t know how to initialize the youtubeids as an array

Read More

so it reads

    'youtubeids'=> array('lbRrePOP2jo','eFAxx17rC0'),

regards

Related posts

Leave a Reply

4 comments

  1. Ok found a solution

    function product_gallery_shortcode($atts) {
    extract(shortcode_atts(array(
                'product_id' => '31',
                'prodvid' => false,
                'youtubeids'=> '',
                'thumbnr' =>2
                    ), $atts));
    etc
    

    and i had to turn youtubeids into an array again

    $youtubeidsnew = array();
    $youtubeidsnew = explode(',', $youtubeids);
    
  2. I found the best solution for this problem. If you want use array for input of shortcode use this:

    function product_gallery_shortcode($atts) {
        extract(shortcode_atts(array(
                    'product_id' => '31',
                    'prodvid' => false,
                    'youtubeids'=> array(),
                    'thumbnr' =>2
                        ), $atts));
        etc 
        $youtubeids = $atts[youtubeids];
        $youtubeids = explode(',', $youtubeids);
    

    In your shortcode input you can use the following for each array index:

    [myshortcode youtubeids="index0,index2,index3"]
    
  3. Simple way I use:

    [my param="key1=value1&key2=value2"]
    

    in shortcode callback, just do:

    parse_str( str_replace("&", "&", $attrs['param']), $array);
    // var_dump( $array );
    
  4. Could you not just do:

    extract(shortcode_atts(array(
                'product_id' => '31',
                'prodvid' => false,
                'youtubeids'=>array('lbRrePOP2jo','eFAxx17rC0'),
                'thumbnr' =>2
            ), $atts));