简体   繁体   English

简码内的简码,嵌套简码?

[英]A Shortcode Within a Shortcode, Nested shortcode?

Problem 问题

I have a plugin called WP-Filebase (see also here ) that allows a user to upload media. 我有一个名为WP-Filebase的插件(另请参见此处 ),该插件允许用户上传媒体。 It has a shortcode to allow the user to publish a download link for that media. 它具有一个简码,允许用户发布该媒体的下载链接。

The shortcode the user would insert into the post is this: 用户将在帖子中插入的简码是这样的:

[wpfilebase tag=file path='EXAMPLE.JPG' tpl=download-button /]

Goal 目标

Replace EXAMPLE.JPG with this shortcode below: 使用下面的此短代码替换EXAMPLE.JPG

 function get_title( ){
   return get_the_title(); 
   }

add_shortcode( 'page_title', 'get_title' );

to make the two short codes look like this essentially: 使两个短代码本质上看起来像这样:

[wpfilebase tag=file path='[page_title]' tpl=download-button /]

WHY? 为什么?

Because the name of the uploaded media (in my case it's images because it's a wallpaper site) matches the name of the post title. 因为上载媒体的名称(在我的情况下是图片,因为它是墙纸网站)的名称与帖子标题的名称匹配。

So if I can enable the second short code to execute within the first short code, I don't have to manually replace EXAMPLE.JPG on my own with a 500+ posts, the short code can do that for me automatically. 因此,如果我可以在第二个短代码中执行第二个短代码,则不必自己用500多个帖子手动替换EXAMPLE.JPG,该短代码可以自动为我完成。

I hit upon a similar problem - I am regularly wanting to nest shortcodes where I couldn't/didn't want to edit the code I was nesting to call do_shortcode(). 我遇到了一个类似的问题-我经常想在不希望/不想编辑要嵌套的代码的dosshortcode()嵌套嵌套的短代码。 So I wrote a generic nesting shortcode routine. 所以我写了一个通用的嵌套简码例程。 It allows you to nest shortcodes using {} instead of [], and requires that you use a couple of other escaped characters in the calls. 它允许您使用{}而不是[]嵌套简码,并要求您在调用中使用其他两个转义字符。 It's below, but to use it in your example you would need to do: 它在下面,但是要在您的示例中使用它,您需要执行以下操作:

[nest shortcode="wpfilebase" tag=file path='{page_title}' tpl=download-button /]

It also allows for parameters to be passed - in these cases you use ^ to represent spaces and ' to represent ". There are a couple of examples in the code comments. 它还允许传递参数-在这种情况下,您使用^表示空格,使用'表示“。代码注释中有几个示例。

I think this is better than wrapping the direct wpfilebase call in your code, just because if you want to change the wpfilebase call (adding parameters, etc) then you don't have to go back into functions.php and mess with the code, you can just do it in the page. 我认为这比将直接wpfilebase调用包装在代码中要好,只是因为如果您想更改wpfilebase调用(添加参数等),那么您就不必回到functions.php并弄乱代码,您可以在页面中进行操作。

Hope this helps! 希望这可以帮助!

add_shortcode('nest', 'shortcode_nest');

function shortcode_nest($atts) {
    // Replace square brackets [] with curly braces {} to nest a shortcode call
    // Use hat ^ instead of spaces when adding parameters to nested calls
    // Use single quote ' instead of double quote " when adding parameters to nested calls
    // Call using [nest shortcode=originalshortcode param="in {getcountryshortcode}"]
    //    to generate [originalshortcode param="in United Kingdom"]
    // or [nest shortcode=originalshortcode content="hello!" param="in {getcountryshortcode}"]
    //    to generate [originalshortcode param="in United Kingdom"]hello![/originalshortcode]
    // or [nest shortcode=originalshortcode content="hello!" param="in {getcountryshortcode^id='94'}"]
    //    to generate [originalshortcode param="in Ireland"]hello![/originalshortcode]
    $shortcode = $atts["shortcode"];
    unset($atts["shortcode"]);
    $stratts = "";
    foreach ($atts as $key => $value) {
        $value = str_replace('{', '[', str_replace('}', ']', $value));
        $value = str_replace('^', ' ', $value);
        $value = str_replace('\'', '"', $value);
        $value = do_shortcode($value);
        if ($key == "content")
            $content = $value;
        else
            $stratts .= "$key='$value' ";
    }
    if (!isset($content))
        $fullcode = "[$shortcode $stratts]";
    else
        $fullcode = "[$shortcode $stratts]$content" . '[/' . $shortcode . ']';
    return do_shortcode($fullcode);
}

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

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