Forum

Passing arguments to shortcode provider

I have put a shortcode provider together for Vimeo. Although any arguments I pass through don't seem to be recognised.

[cms:vimeo 191465923 width=1920]

class shortcode_Vimeo extends PerchShortcode_Provider {
    public $shortcodes = ['vimeo'];

    public function get_shortcode_replacement($Sortcode, $Tag)
    {
        $id = $Sortcode->arg(0);

        $API = new PerchAPI(1.0, 'shortcode_vimeo');
        $HTTP = $API->get('HTTPClient');

        $response = $HTTP->get('https://vimeo.com/api/oembed.json?url='.urlencode('https://vimeo.com/'.$id));

        if ($response) {            
            $data = json_decode($response, true);
            if (isset($data['html'])) return $data['html'];
        }

        return '';
    }
}

Would I change $id = $Sortcode->arg(0); to $id = $Sortcode->arg(0).$Sortcode->arg('width'); ?

Although, what if I wanted to add other arguments like autoplay, color, title ?

Thanks

Marc Sanders

Marc Sanders 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

$id = $Sortcode->arg(0); is reading the first argument and assigning it to $id. It doesn't make sense to concatenate anything to it.

$Sortcode->arg('width') should be the value of the width attribute, if set.

OK, so say if I waned to add arguments to the shortcode like [cms:vimeo 191465923 width=1920 autoplay=true color=#4971b0]

Sometimes autoplay or colour may not be needed. How would I collect all the arguments and set them on the request. Would something like this work?

$args = array();

if (isset($Sortcode->arg('width'))) $args['width'] = 'width='.$Sortcode->arg('width');
if (isset($Sortcode->arg('autoplay'))) $args['autoplay'] = 'autoplay='.$Sortcode->arg('autoplay');
if (isset($Sortcode->arg('color'))) $args['color'] = 'color='.$Sortcode->arg('color');

$query = '';

if (!empty($args)) {
    $query = '?'.implode('&', $args);
}

$response = $HTTP->get('https://vimeo.com/api/oembed.json?url='.urlencode('https://vimeo.com/'.$id.$query));

Updated the code, although doesn't seem to make a difference to the returned embed:

Anything standing out

class Shortcode_Vimeo extends PerchShortcode_Provider {
    public $shortcodes = ['vimeo'];

    public function get_shortcode_replacement($Sortcode, $Tag) {
        $id = $Sortcode->arg(0);

        $args = $Sortcode->get_args();

        array_shift($args);

        $query = '';

        if (!empty($args)) {
            $query = '?'.http_build_query($args);
        }

        $API = new PerchAPI(1.0, 'shortcode_vimeo');
        $HTTP = $API->get('HTTPClient');

        echo 'https://vimeo.com/api/oembed.json?url='.urlencode('https://vimeo.com/'.$id.$query);

        $response = $HTTP->get('https://vimeo.com/api/oembed.json?url='.urlencode('https://vimeo.com/'.$id.$query));

        if ($response) {            
            $data = json_decode($response, true);
            if (isset($data['html'])) return $data['html'];
        }

        return '';
    }
}
Drew McLellan

Drew McLellan 2638 points
Perch Support

I'm afraid can't offer support for this.

Cool, no worries

Thanks