Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm setting up a plugin with a nested shortcode, i.e.

[ct_training_group]
    [ct_training]
[/ct_training_group]

Here's the catch: I use the [ct_training] code outside of the parent as well as within it, and want to output something slightly different in each case. When it is inside a group, I need to omit the <ul> and </ul> that I would wrap the [ct_training] shortcode in when it is called on its own.

I figure the easiest way to do this would be to pass a variable from the parent to the child. Is there any graceful way to do this where I wouldn't need to mess with globals or such?

Thanks.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

I assume you just pass the content of ct_training_group to another call of do_shortcode()? You can't pass extra parameters to it, so if you don't want to use global state variables, you could always replace the current shortcode handler for ct_training with one that doesn't add the extra <ul>. It seems there is no cache for the handlers, so there will not be a performance penalty.

add_shortcode('ct_training', 'ct_training_full');
function ct_training_full($attr, $content)
{
    return '<ul>' . ct_training_item($attr, $content) . '</ul>';
}

function ct_training_item($attr, $content)
{
    return '<li>CT Training</li>';
}

add_shortcode('ct_training_group', 'ct_training_group_handler');
function ct_training_group_handler($attr, $content)
{
    $output = '<h3>CT Training group</h3>';
    $output .= '<ul>';
    // Redefine the sub-code
    add_shortcode('ct_training', 'ct_training_item');
    $output .= do_shortcode($content);
    // Reset the sub-code
    add_shortcode('ct_training', 'ct_training_full');
    $output .= '</ul>';
    return $output;
}
share|improve this answer
    
Perfect. Thanks much, Jan. –  jdp Nov 15 '10 at 16:37
    
Nice, clean idea. –  Raphael Nov 16 '10 at 14:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.