2

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.

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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