WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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?


share|improve this question
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

Your Answer


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.