5

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

so it reads

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

regards

2
  • 2
    I'm sorry, but I don't understand what you goal is. Could you elaborate? Currently the variable $youtubeids holds that array... Commented Mar 5, 2013 at 11:49
  • @kaiser, well when i use the shortcode the values i passes into youtubeids didn't work as an array in my shortcode function. So i had to turn the values into an array again, see my answer Commented Mar 5, 2013 at 12:16

4 Answers 4

4

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
  • 2
    It is not clear how this 'fixes' your problem ( or what your problem actually was ). Could you ellaborate so that this answer may be helpful to other people? Commented Mar 5, 2013 at 12:15
  • As you goal is completely unclear (what is the expected output?), it's impossible to understand your "solution". I'm holding back from a downvote, but please elaborate your goal better (edit question) and then elaborate why this is a fix. Commented Mar 5, 2013 at 12:21
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"]
1

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 );
0

Could you not just do:

extract(shortcode_atts(array(
            'product_id' => '31',
            'prodvid' => false,
            'youtubeids'=>array('lbRrePOP2jo','eFAxx17rC0'),
            'thumbnr' =>2
        ), $atts));
2
  • yes that what i did staticly but the shortcode looks something like this: [productgallery product_id="7" prodvid="yes" youtubeids="lbRfgPOP2jo,eFAEfg7rC0" thumbnr="2"] Commented Mar 5, 2013 at 12:12
  • 3
    I'm sorry but there simply isn't enough information to answer then. It's not clear what your problem is, or what an answer would be. We'd need code Commented Mar 5, 2013 at 12:14

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.