D.Shinekhuu
D.Shinekhuu

Reputation: 307

Create shortcode with parameter in PHP Joomla

I've created a simple shortcode plugin on Joomla. Actually I am trying to integrate Cleeng Video with Joomla. And will connect it's users in the future ( I hope ). I've stack on creating shortcode's parameter. I don't know how to parse it's parameter and value. My Shortcode is here (no parameter)

{cleengvideo}<iframe class="wistia_embed" src="http://fast.wistia.net/embed/iframe/5r8r9ib6di" name="wistia_embed" width="640" height="360" frameborder="0" scrolling="no" allowfullscreen=""></iframe>{/cleengvideo}

My code is here

public function onContentPrepare($content, $article, $params, $limit) {
     preg_match_all('/{cleengvideo}(.*?){\/cleengvideo}/is', $article->text, $matches);
      $i = 0;
      foreach ($matches[0] as $match) {
      $videoCode = $matches[1][$i];
      $article->text = str_replace($match, $videoCode, $article->text);
      }

I want to set height, width and 5r8r9ib6di this code from shortcode at least. Please can anyone help me with adding and parsing it's parameter

Upvotes: 0

Views: 1991

Answers (3)

BhavyaSoft
BhavyaSoft

Reputation: 41

Hope this helps someone searching for shortcode parameters, for parameters in short code we can use preg_match_all like that

preg_match_all('/{cleengvideo(.*?)}(.*?){\/cleengvideo}/is', $article->text, $matches);

This will give a array with 3 array elements, the second array have the parameters which you can maupulate with codes.

Hope this helps.

Upvotes: 0

D.Shinekhuu
D.Shinekhuu

Reputation: 307

I think I could found it's solution. It's here https://github.com/Cleeng/cleeng-wp-plugin/blob/master/php/classes/Frontend.php Code is

$expr = '/\[cleeng_content(.*?[^\\\])\](.*?[^\\\])\[\/cleeng_content\]/is';
        preg_match_all( $expr, $post->post_content, $m );
        foreach ( $m[0] as $key => $content ) {
            $paramLine = $m[1][$key];
            $expr = '/(\w+)\s*=\s*(?:\"|&quot;)(.*?)(?<!\\\)(?:\"|&quot;)/si';
            preg_match_all( $expr, $paramLine, $mm );

            if ( ! isset( $mm[0] ) || ! count( $mm[0] ) ) {
                continue;
            }

            $params = array( );
            foreach ( $mm[1] as $key => $paramName ) {
                $params[$paramName] = $mm[2][$key];
            }
            if ( ! isset( $params['id'] ) ) {
                continue;
            }

            $content = array(
                'contentId' => $params['id'],
                'shortDescription' => @$params['description'],
                'price' => @$params['price'],
                'itemType' => 'article',
                'purchased' => false,
                'shortUrl' => '',
                'referred' => false,
                'referralProgramEnabled' => false,
                'referralRate' => 0,
                'rated' => false,
                'publisherId' => '000000000',
                'publisherName' => '',
                'averageRating' => 4,
                'canVote' => false,
                'currencySymbol' => '',
                'sync' => false
            );

            if ( isset( $params['referral'] ) ) {
                $content['referralProgramEnabled'] = true;
                $content['referralRate'] = $params['referral'];
            }

            if ( isset( $params['ls'] ) && isset( $params['le'] ) ) {
                $content['hasLayerDates'] = true;
                $content['layerStartDate'] = $params['ls'];
                $content['layerEndDate'] = $params['le'];
            }

            $this->cleeng_content[$params['id']] = $content;
        }

Upvotes: 0

Lodder
Lodder

Reputation: 19733

To get a parameter, you can simply use the following code:

$params->get('param_name', 'default_value');

So for example, in your XML file, if you had a field like so:

<field name="width" type="text" label="Width" default="60px" />

you would call the parameter like so:

$params->get('width', '60px');

Note that you don't have to add the default value as the second string, however I always find it good practice.

Hope this helps

Upvotes: 1

Related Questions