1

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.

3

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;
}
  • Nice, clean idea. – Raphael Nov 16 '10 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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