在短码插件的模板中使用自定义短码

时间:2021-04-12 作者:commadelimited

我已经编写了一个自定义插件,ta-intentclicks 用作短代码:

[ta-intentclicks count="3" category="SEC-EDR"...]
在这个短代码中,我想使用另一个可以用作助手的短代码。例如在我插件中的一个PHP模板中。

[ta-intentclicks-link url="$list_item[\'link\']"]Visit website[/ta-intentclicks-link]
[ta-intentclicks-link url="$list_item[\'link\']"]<img src="foo" />[/ta-intentclicks-link]
将输出以下内容:

<a href="<the URL>" rel="sponsored" target="_blank" class="icp-list-link">Visit website</a>
<a href="<the URL>" rel="sponsored" target="_blank" class="icp-list-link"><img src="foo" /></a>
这里有一个快速的目录快照来帮助说明我的问题。enter image description here

插件入口点是includes/class-ta-intentclicks.php 它定义并运行短代码,同时调用Layout类。

class TaIntentClicks {

    function __construct() {
        add_shortcode(\'ta-intentclicks\', array($this, \'run\'));
    }

    function run($attributes = []) {
        ... do some stuff
        return $this->layout->render($response, $layoutAttributes, $dataAttributes);
    }

}

class TAIntentClicksLayout {
    function parse($stuff, $template) {

        ob_start();
        $output = \'\';
        include $template;
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }

    function render($response, $layoutAttributes, $dataAttributes) {

        return $this->parse(
            $response,
            $layoutAttributes,
            $this->getTemplate($layoutAttributes),
            $dataAttributes
        );
    }
}
我见过有人打电话给;do\\u shortcode“do\\u shortcode”;执行一个短代码,但我不清楚在我的情况下,短代码函数在哪里,或者在哪里放置;do\\u shortcode“do\\u shortcode”;呼叫

这里有人能提供指导吗?这是我的第一个插件,对于如何实现这个功能,我有点不知所措。提前谢谢。

3 个回复
SO网友:Sally CJ

If I understand it correctly, inside the shortcode template, you could do something like this:

echo do_shortcode( \'[ta-intentclicks-link url="\' . esc_url( $list_item[\'link\'] ) . \'"]Visit website[/ta-intentclicks-link]\' );

But then, instead of having to use do_shortcode(), you could actually simply call the shortcode callback from the (shortcode) template ( which then eliminates the need to find and parse shortcodes in the content or the first parameter for do_shortcode() ):

  • If you registered the shortcode like so:

    add_shortcode( \'ta-intentclicks-link\', \'my_shortcode\' );
    function my_shortcode( $atts = array(), $content = null ) {
        $atts = shortcode_atts( array(
            \'url\' => \'\',
        ), $atts );
    
        if ( ! empty( $atts[\'url\'] ) ) {
            return sprintf( \'<a href="%s" target="_blank">%s</a>\',
                esc_url( $atts[\'url\'] ), esc_html( $content ? $content : $atts[\'url\'] ) );
        }
    
        return \'\';
    }
    
  • Then in the shortcode template, you could simply call the my_shortcode() function above:

    echo my_shortcode( array(
        \'url\' => $list_item[\'link\'],
    ), \'Visit website\' );
    

But if that\'s not what you meant, or if you had a shortcode in a shortcode like [caption]Caption: [myshortcode][/caption], then as said in the Shortcode API on the WordPress Codex website:

If the enclosing shortcode is intended to permit other shortcodes in its output, the handler function can call do_shortcode() recursively:

function caption_shortcode( $atts, $content = null ) {
  return \'<span class="caption">\' . do_shortcode($content) . \'</span>\';
}

... your run() function (or the shortcode callback) would need to capture the second parameter passed to the function, i.e. $content as you could see above, and once you have that parameter (or its value), you can then call do_shortcode().

(With the above "caption" shortcode example, the $content is the Caption: [myshortcode].)

So for example, your run() function be written like so…

// 1. Define the $content variable.
function run( $attributes = [], $content = null ) {
    // ... your code (define $response, $layoutAttributes, $dataAttributes, etc.)

    // 2. Then do something like so:
    return $this->layout->render( $response, $layoutAttributes, $dataAttributes ) .
        do_shortcode( $content );
}

But if you need further help with that, do let me know in the comments (and preferably, please show the code in your template and explain the variables like the $response and $layoutAttributes).

SO网友:DACrosby

function parse($stuff, $template) {
    ob_start();
    $output = \'\';
    include $template;
    $output = ob_get_contents();
    ob_end_clean();
    $output = do_shortcode($output); // This line will parse shortcodes from $template
    return $output;
}
添加一些细节:您的主要短代码,[ta-intentclicks], 我想你会把它放在页面/帖子内容中。WordPress运行do_shortcode 在…上the_content 所以这一切都如期而至。

do_shortcode 不递归运行短代码,这意味着嵌套的短代码只显示为括在括号中的纯文本/它们不呈现短代码内容。所以,你需要跑步do_shortcode 再次对您的短代码内容进行下一步处理;“级别”;短代码的。同样地,如果您的内部有一个短代码[ta-intentclick], 你需要跑步do_shortcode 第二次(第三次,如果你算上WP的核心运行)。

我不确定这种行为是否有明确的记录,但如果您通读do_shortcode source code here 很明显,它不处理子短码。

SO网友:Tiyo

为什么不使用这个简单的代码来实现呢?我的意思是,它不需要在短代码中包含短代码。

function links( $atts, $content = null ){
    $args = shortcode_atts( array(
        \'s\' => \'\', //name
        \'l\' => \'\', //link
    ), $atts);

    return \'<a href="\'. $args[\'l\'] .\'">\'. $args[\'s\'] .\'</a>\';
}
add_shortcode (\'link\', \'links\');
你可以写[link s="NAME" l="LINK"]而且更简单。

相关推荐

Wordpress Vue Js Shortcodes

目前,我正在与Vue一起开发wordpress网站。js和wordpress REST API,找不到使用wordpress ShortCodeEx的解决方案:Contact Form 7 我一直在尝试这个例子,但它似乎对我不起作用Vue.js + AJAX Shortcode有没有人有过类似的问题?编辑:For ExampleIn regular wordpress我可以用这种方式使用联系人表单插件 <section class=\"container section__form display-