Execute shortcode only in another shortcode

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

Leave a Comment