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 wonder if it's possible to do a shortcode only in another shortcode. For example I got following code pieces:

[heading]some pricing page[/heading]
[pricing_box]
  [heading]Some title[/heading]
[/pricing_box]

Now I don't want to define one shortcode heading which would apply to both the nested one and the first level shortcode. I want the both heading shortcodes to produce different outputs. Of course, I could try to solve this by renaming one of the shortcodes to title or something, but this doesn't always result in optimal naming arrangements.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

To use a different callback for the nested headings switch the shortcode handler in the pricing_box callback and restore the main handler before you return the string.

add_shortcode( 'pricing_box', 'shortcode_pricing_box' );
add_shortcode( 'heading',     'shortcode_heading_main' );

function shortcode_pricing_box( $atts, $content = '' )
{
    // switch shortcode handlers
    add_shortcode( 'heading', 'shortcode_heading_pricing_box' );

    $out = '<div class="pricing-box">' . do_shortcode( $content ) . '</div>';

    // restore main handler
    add_shortcode( 'heading', 'shortcode_heading_main' );

    return $out;
}

function shortcode_heading_main( $atts, $content = '' )
{
    return "<h2>$content</h2>";
}

function shortcode_heading_pricing_box( $atts, $content = '' )
{
    return "<h3>$content</h3>";
}
share|improve this answer
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.