繁体   English   中英

在PHP Joomla中使用参数创建简码

[英]Create shortcode with parameter in PHP Joomla

我在Joomla上创建了一个简单的简码插件。 实际上,我正在尝试将Cleeng Video与Joomla集成。 并且将来会与它的用户建立联系(我希望)。 我已经在创建shortcode的参数上堆叠了。 我不知道如何解析它的参数和值。 我的简码在这里(无参数)

{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}

我的代码在这里

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

我想至少从shortcode设置此代码的高度,宽度和5r8r9ib6di。 请任何人可以帮助我添加和解析它的参数

要获取参数,您只需使用以下代码即可:

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

因此,例如,在您的XML文件中,如果您具有如下所示的字段:

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

您将这样调用参数:

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

请注意,您不必将默认值添加为第二个字符串,但是我总是觉得它是一种好习惯。

希望这可以帮助

我想我可以找到解决方案。 在这里https://github.com/Cleeng/cleeng-wp-plugin/blob/master/php/classes/Frontend.php代码是

$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;
        }

希望这可以帮助搜索短代码参数的人,对于短代码中的参数,我们可以像这样使用preg_match_all

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

这将提供一个包含3个数组元素的数组,第二个数组具有可以用代码进行伪造的参数。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM