Execute shortcode only in another shortcode

I wonder if it’s possible to do a shortcode only in another shortcode. For example I got following code pieces:

<div class="su-heading su-heading-style-default su-heading-align-center" id="" style="font-size:13px;margin-bottom:20px"><div class="su-heading-inner">some pricing page</div></div>
[pricing_box]
  <div class="su-heading su-heading-style-default su-heading-align-center" id="" style="font-size:13px;margin-bottom:20px"><div class="su-heading-inner">Some title</div></div>
[/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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x