简体   繁体   中英

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. 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:

 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.

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(). 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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